如何在新线程中运行静态函数?

use*_*802 2 c++ multithreading

通过论坛搜索后,我遇到了一些答案nevertheles我无法得到如何在c ++的新线程中运行静态方法的明确答案.我主要担心的是启动线程的最佳方法是什么?(它是否也可以从另一个线程内部工作?)哪个头更好用?thread.h,pthread.h?

我想创建一个新线程(当调用给定的方法时)并在此线程内调用另一个函数...任何提示我如何处理这个问题?

非常感谢你们提前!

Ros*_*ost 7

在线程中运行静态成员函数没有问题.只需使用std::thread与自由功能相同的方式:

#include <thread>

class Threaded
{
public:

   static void thread_func() {}

};

int main()
{
    std::thread t(Threaded::thread_func);
    t.join();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当然,启动线程也可以从任何其他线程工作.使用符合C++ 11标准的编译器#include <thread>.否则看一看boost::thread.它的用法类似.