首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用C++11的线程基类

使用C++11的线程基类
EN

Stack Overflow用户
提问于 2015-07-01 19:34:34
回答 1查看 1.4K关注 0票数 2

由于我是C++11新手,所以我正在寻找使用C++11多线程特性适当地实现线程基类的方法,将参数传递给类,启动和停止线程.。就像这样:http://www.codeproject.com/Articles/21114/Creating-a-C-Thread-Class,但是通过C++11来获得操作系统的独立性。

我在谷歌上搜索过,但没有发现任何有用的东西。也许有人熟悉一个好的开源实现?

编辑来精确地解释我的问题:我已经知道了std::thread,但我的目的分别是使用一个包装类,以使std::thread不必过多地处理它。我目前正在使用下面的类结构(从1年起)。但是我分别被限制在Windows上,这不是我想要的。

代码语言:javascript
复制
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;
};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-01 19:40:14

看看std::线程

代码语言:javascript
复制
#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();
}

您需要的所有线程支持都可以找到这里

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31170127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档