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

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

我不记得Boost.Thread的细节,但总的想法是这样的:

class thread_function_base { public: virtual ~thread_function_base(void) {} virtual void run(void) = 0; }; template  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  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  thread(const Func& pFunc) { std::auto_ptr threadFunc(new thread_function_0(pFunc)); create_thread(threadFunc); } template  thread(const Func& pFunc, const A0& pA0) { std::auto_ptr threadFunc(new thread_function_1(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 pFunc(static_cast(pUserData)); // (A) pFunc->run(); } void create_thread(std::auto_ptr& 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最终将删除它。)

等等,使其更加复杂。

您必须围绕pthread_create创建一个包装器,并将特定于线程的存储传递给包含参数数组的pthread_create。 包装器将使用如下forms:

 void *mythreadfunction(void *arg) {...} pthread_create_wrapper(context ctx, ...) { Array *arr; pthread_t mythread; arr = new Array(); // push arguments to array here blah blah ... // create thread and pass in argument list as thread data pointer. pthread_create(&mythread, NULL, mythreadfunction, (void *)arr); }