C++11多线程编程 -- thread

std::thread类

#include 

namespace std 
{
    class thread
    {
    public:
        // construct/copy/destroy:
        thread() noexcept;

        thread(thread&& other) noexcept;

        template<class Function, class... Args>
        explicit thread(Function&& f, Args&&.. args);

         ~thread();

        thread(const thread&) = delete;
        thread(thread&&) noexcept;

        thread& operator=(const thread&) = delete;
        thread& operator=(thread&&) noexcept;

        // Observers
        bool joinable() const noexcept;
        id get_id() const noexcept;

        native_handle_type native_handle();
        static unsigned int hardware_concurrency() noexcept;

        // Operations
        void join();
        void detach();
        void swap( thread& other ) noexcept;

    public:
        // member
        typedef pthread native_handle_type; //linux
        class id;
    };

    // no-member function
    void swap( thread &lhs, thread &rhs ) noexcept;
}

C++11中创建线程,使用std::thread类即可,构造thread对象时传入一个可调用对象(如果有参数,把参数同时传入),构造完成后,新的线程马上就被创建了,并马上开始执行该可调用对象。这里的可调用对象可以是普通函数,函数对象,Lambada表达式。

std::thread默认的构造函数构造的对象不关联任何线程;判断一个thread对象是否关联某个线程,使用joinable()接口,如果返回true,表明该对象关联着某个线程(即使该线程已经执行结束);

“joinable”的对象析构前,必须调用join()接口等待线程结束,或者调用detach()接口解除与线程的关联,否则会抛异常。 即如果不想join或detach则不能让thread对象析构。

正在执行的线程从关联的对象detach后会自主执行直至结束,对应的对象变成不关联任何线程的对象,joinable()将返回false;

std::thread没有拷贝构造函数和拷贝赋值操作符,因此不支持复制操作(但是可以move),也就是说,没有两个 std::thread对象会表示同一执行线程;

容易知道,如下几种情况下,std::thread对象是不关联任何线程的(对这种对象调用join或detach接口会抛异常):
- 默认构造的thread对象;
- 被移动后的thread对象;
- detach 或 join 后的thread对象;

示例

#include 
#include 
#include 
#include 

void f1(int n)
{
    for (int i = 0; i < 5; ++i) {
        std::cout << "Thread 1 executing\n";
        ++n;
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}

void f2(int& n)
{
    for (int i = 0; i < 5; ++i) {
        std::cout << "Thread 2 executing\n";
        ++n;
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}

int main()
{
    int n = 0;
    std::thread t1; // t1 is not a thread
    std::thread t2(f1, n + 1); // pass by value
    std::thread t3(f2, std::ref(n)); // pass by reference
    std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread
    t2.join();
    t4.join();
    std::cout << "Final value of n is " << n << '\n';
}

输出:

Thread 1 executing
Thread 2 executing
Thread 1 executing
Thread 2 executing
Thread 1 executing
Thread 2 executing
Thread 1 executing
Thread 2 executing
Thread 2 executing
Thread 1 executing
Final value of n is 5

头文件thread其他可用函数

namespace std
{
    //当前线程的操作
    namespace this_thread
    {
        //返回当前线程的线程id
        std::thread::id get_id() noexcept;

        //当前线程休眠一段时间
        template< class Rep, class Period >
        void sleep_for( const std::chrono::duration& sleep_duration );

        //暂停当前线程到指定时间点
        template< class Clock, class Duration >
        void sleep_until( const std::chrono::time_point& sleep_time );

        //让出CPU使用权,重新调度
        void yield() noexcept;
    }
}

你可能感兴趣的:(C/C++)