gpt4 book ai didi

c++ - 创建复制传递给它们的参数的线程

转载 作者:行者123 更新时间:2023-11-28 08:27:22 24 4
gpt4 key购买 nike

我目前正在使用 boost::thread,因为它非常方便地允许我将任意数量的参数传递给线程并沿途复制它们,所以我不必担心它们在执行之前被删除线程启动。是否有任何其他库允许这样做,或使用 pthreads 模拟它的方法?我想让自己摆脱 boost,但我从未见过任何其他图书馆这样做。

最佳答案

我不记得 Boost.Thread 的细节,但大致的思路是这样的:

class thread_function_base
{
public:
virtual ~thread_function_base(void) {}
virtual void run(void) = 0;
};

template <typename Func>
class thread_function_0 : public thread_function_base
{
public:
thread_function_0(const Func& pFunc) :
mFunc(pFunc)
{}

void run(void)
{
mFunc();
}

private:
Func mFunc;
};

template <typename Func, typename A0>
class thread_function_1 : public thread_function_base
{
public:
thread_function_1(const Func& pFunc, const A0& pA0) :
mFunc(pFunc),
mA0(pA0)
{}

void run(void)
{
mFunc(mA0);
}

private:
Func mFunc;
A0 mA0;
};

// and so on to some limit, either
// generated either by hand (yuck), by
// Boost.PP (phew), or by C++0x's
// variadic templates (yay, no limit either)

class thread
{
public:
template <typename Func>
thread(const Func& pFunc)
{
std::auto_ptr<thread_function_base>
threadFunc(new thread_function_0<Func>(pFunc));

create_thread(threadFunc);
}

template <typename Func, typename A0>
thread(const Func& pFunc, const A0& pA0)
{
std::auto_ptr<thread_function_base>
threadFunc(new thread_function_1<Func, A0>(pFunc, pA0));

create_thread(threadFunc);
}

// again, needs to be generated somehow

private:
// noncopyable
thread(const thread&);
thread& operator=(const thread&);

// signature needs to match implementations expectations:
static void thread_function(void* pUserData)
{
std::auto_ptr<thread_function_base>
pFunc(static_cast<thread_function_base*>(pUserData));

// (A)

pFunc->run();
}

void create_thread(std::auto_ptr<thread_function_base>& pThreadFunc)
{
// again, implementation specific function:
if (create_thread(&thread_function, pThreadFunc.get(), ...))
{
// failed, do something (and return),
// auto_ptr in constructor will free resources
return;
}

// thread was created, so it now owns that resource
pThreadFunc.release();

// (B)
}
};

基本上,调用线程所需的一切都被复制到一些动态分配的容器中,指向该动态容器的指针被传递到线程函数中(微不足道),然后所有权从线程外部转移到线程内部。

您不仅可以将 thread_function_base 打包到用户数据中,还可以将(特定于实现的)信号句柄打包,从而使事情变得更安全。线程函数将在 (A) 处阻塞,直到在 (B) 处发出信号,表明主线程已将资源的全部所有权授予工作线程。 (从那里 auto_ptr 最终会删除它。)

等等,使其更加复杂。

关于c++ - 创建复制传递给它们的参数的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3497861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com