簡體   English   中英

使用C ++ 11的線程基類

[英]Thread base class using C++11

因為我是C ++ 11的新手,所以我正在尋找使用C ++ 11多線程功能,將參數傳遞給類,啟動和停止線程...的線程基類的正確實現。 像這樣的東西: http : //www.codeproject.com/Articles/21114/Creating-aC-Thread-Class,但是使用C ++ 11獲得了OS獨立性。

我已經用谷歌搜索了,但是沒有發現任何有用的東西。 也許有人熟悉良好的開源實現?

編輯為了精確地解釋我的問題:我已經知道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 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();
}

您需要的所有線程支持都可以在這里找到。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM