由于我是C++11新手,所以我正在寻找使用C++11多线程特性适当地实现线程基类的方法,将参数传递给类,启动和停止线程.。就像这样:http://www.codeproject.com/Articles/21114/Creating-a-C-Thread-Class,但是通过C++11来获得操作系统的独立性。
我在谷歌上搜索过,但没有发现任何有用的东西。也许有人熟悉一个好的开源实现?
编辑来精确地解释我的问题:我已经知道了std::thread,但我的目的分别是使用一个包装类,以使std::thread不必过多地处理它。我目前正在使用下面的类结构(从1年起)。但是我分别被限制在Windows上,这不是我想要的。
class ThreadBase {
public:
ThreadBase();
virtual ~ThreadBase();
// this is the thread run function, the "concrete" threads implement this method
virtual int Run() = 0;
// controlling thread behaviour, Stop(), Resume() is not that necessary (I implemented it, beacuse the API gives me the opportunity)
virtual void Start() const;
virtual void Stop() const;
// returns a duplicated handle of the thread
virtual GetDuplicateHdl() const; //does std::thread give me something similar to that?
protected:
// return the internal thread handle for derived classes only
virtual GetThreadHdl() const;
//...block copy constructor and assignment operator
private:
// the thread function
void ThreadFunc(void * Param); //for Windows the return value is WINAPI
//THandleType? ThreadHdl;
unsigned long ThreadId;
};发布于 2015-07-01 19:40:14
看看std::线程
#include <iostream>
#include <thread>
void function()
{
std::cout << "In Thread\n";
}
int main()
{
std::thread x(function);
// You can not let the object x be destroyed
// until the thread of execution has finished.
// So best to join the thread here.
x.join();
}您需要的所有线程支持都可以找到这里。
https://stackoverflow.com/questions/31170127
复制相似问题