使用C++11线程基类
Thread base class using C++11
由于我是C++11的新手,我正在寻找一个使用C++11多线程功能的线程基类的正确实现,该功能将参数传递给类,启动和停止线程。所以像这样的事情:http://www.codeproject.com/Articles/21114/Creating-a-C-Thread-Class但是使用C++11来获得操作系统独立性。
我在谷歌上搜索过,但没有发现任何有用的东西。也许有人熟悉一个好的开源实现?
编辑为了准确地解释我的问题:我已经知道std::thread
,但我的意图和目标分别是为std::thread
使用一个包装器类,以避免大量处理它。我现在使用下面的类结构(从1年开始)。但我被分别绑定到Windows API,这不是我想要的。
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;
};
查看std::thread
#include <iostream>
#include <thread>
void function()
{
std::cout << "In Threadn";
}
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();
}
您需要的所有线程支持都可以在这里找到。
相关文章:
- 类与私有变量的其他类之间的线程安全性
- 使用 thread 类在 C++ 中构造线程的动态数组时出错
- 使用基类中的派生方法运行线程,而无需使用模板
- 静态 constexpr 类成员变量对多线程读取是否安全?
- 使用 ubuntu 终端在 c++ 上运行线程类的问题
- 如何声明由多个线程调用的 C++ DLL 的内部类,而无需导出类
- 如何将类的成员函数传递给线程
- 启动类函数作为失去引用的线程
- 将类成员函数作为线程调用到另一个类成员函数时发出警告消息
- 锁定来自其他线程的类成员
- 提供对不同类型的数据(建议、代码审查)的线程安全访问的类
- C++多线程程序:变量定义为类成员的隔离错误
- 如何将对象从类线程传递到帖子?
- 尝试在我的基类中启动线程时,Visual Studio 由于调试错误而中止我的程序
- 在从基类构造函数创建的单独线程中调用纯虚函数
- 在 C++11 中,是否可以将 ref'ed 基类传递给线程的构造函数并获得多态行为?
- 带有纯虚拟回调的线程化基类,在破坏c++时停止
- 当在不同的索引上写入时,这个矩阵类线程安全吗?
- 类线程安全的静态const char[]
- 使用C++11线程基类