c++11学習-thread基本使用

10798 ワード

qtはc++11をサポートし、QMAKE_を追加するCXXFLAGS += -std=c++0x CONFIG += C++11
1.関数を入れる
#include 
#include 
#include 
#include 

void threadFunc(int num)
{
    for(int i = 0; i < 3; ++i)
    {
        std::cout << "i = " << i
                  << " num = " << num
                  << std::endl;
    }
}

int main(void)
{
    std::shared_ptr<std::thread> thFunc = \
            std::make_shared<std::thread>(threadFunc,3);
    std::cout << "wait thread" << std::endl;
    thFunc->join();
    std::cout << "main end" << std::endl;
    return 0;
}

2.クラスに入れる
#include 
#include 
#include 
#include 

class ThreadFunc
{
public:
    void operator()(int num)
    {
        this->threadFunc(num);
    }

    void threadFunc(int num)
    {
        for(int i = 0; i < 3; ++i)
        {
            std::cout << "i = " << i
                      << " num = " << num
                      << std::endl;
        }
    }
};

int main(void)
{
    std::shared_ptr<std::thread> thFunc = \
            std::make_shared<std::thread>(ThreadFunc(),3);
    std::cout << "wait thread" << std::endl;
    thFunc->join();
    std::cout << "main end" << std::endl;
    return 0;
}

3.オブジェクトを入れる
#include 
#include 
#include 
#include 

class ThreadFunc
{
public:
    void operator()(int num)
    {
        this->threadFunc(num);
    }

    void threadFunc(int num)
    {
        for(int i = 0; i < 3; ++i)
        {
            std::cout << "i = " << i
                      << " num = " << num
                      << std::endl;
        }
    }
};

int main(void)
{
    std::shared_ptr pTFunc = \
            std::make_shared();
    std::shared_ptr<std::thread> thFunc = \
            std::make_shared<std::thread>(*pTFunc,3);
    std::cout << "wait thread" << std::endl;
    thFunc->join();
    std::cout << "main end" << std::endl;
    return 0;
}

4.メンバー関数の挿入
#include 
#include 
#include 
#include 

class ThreadFunc
{
public:
    void threadFunc(int num)
    {
        for(int i = 0; i < 3; ++i)
        {
            std::cout << "i = " << i
                      << " num = " << num
                      << std::endl;
        }
    }
};

int main(void)
{
    std::shared_ptr pTFunc = \
            std::make_shared();
    std::shared_ptr<std::thread> thFunc = \
            std::make_shared<std::thread>(&ThreadFunc::threadFunc,*pTFunc,3);
    std::cout << "wait thread" << std::endl;
    thFunc->join();
    std::cout << "main end" << std::endl;
    return 0;
}

5.クラス内での使用
#include 
#include 
#include 
#include 

class ThreadFunc
{
public:
    void run()
    {
        th = std::make_shared<std::thread>(&threadFunc,*this,3);
        th->join();
    }

    void threadFunc(int num)
    {
        for(int i = 0; i < 3; ++i)
        {
            std::cout << "i = " << i
                      << " num = " << num
                      << std::endl;
        }
    }
private:
    std::shared_ptr<std::thread> th;
};

int main(void)
{
    std::shared_ptr pTFunc = \
            std::make_shared();
    pTFunc->run();
    std::cout << "main end" << std::endl;
    return 0;
}

6.スレッドブレーク