美文网首页
c++ 11并发之线程运行管理【二】

c++ 11并发之线程运行管理【二】

作者: nyhoo | 来源:发表于2018-06-05 23:46 被阅读0次

    线程的启动

    • std::thread可以与任何可调用类型一同工作,所以可以传递函数,lamda表达式,带有函数操作符的类实例等进行构造thread。
    #include <iostream>  
    #include <functional>  
    #include <thread>  
    #include <string>  
    using namespace std;  
      
    void call_fun(int n)  
    {  
        //for(int i=0;i<100;i++)  
        {  
            cout<<n<<endl;  
        }  
    }  
      
    class test  
    {  
    public:  
        void operator()()  
        {  
            cout<<"Class::operator"<<endl;  
        }  
      
    };  
      
      
    int main(int argc, char *argv[])  
    {  
        std::thread th_fun(call_fun,123);  
        th_fun.join();  
      
        std::thread th_lamda([]{cout<<"lamda function"<<endl;});  
        th_lamda.join();  
      
        std::thread th_class_operator((test()));  
        th_class_operator.join();  
      
        return 0;  
    }  
    

    等待线程完成

    • 使用join等待线程运行完成,注意异常情况下的等待
    #include <iostream>  
    #include <functional>  
    #include <thread>  
    #include <string>  
    #include <stdio.h>  
      
    using namespace std;  
      
    void calc_fun(int n)  
    {  
        int sum=0;  
        for(int i=0;i<n;i++)  
        {  
            sum+=i;  
        }  
        cout<<"calc ret:"<<sum<<endl;  
    }  
      
      
    class thread_guard  
    {  
    public:  
        thread_guard(const thread_guard&)=delete;  
        thread_guard& operator =(const thread_guard&)=delete;  
        explicit thread_guard(std::thread& __t):__th(__t)  
        {  
      
        }  
        ~thread_guard()  
        {  
            if(__th.joinable())  
            {  
                __th.join();  
            }  
        }  
    private:  
        std::thread& __th;  
    };  
      
      
    int main(int argc, char *argv[])  
    {  
        thread th1(calc_fun,10000000);  
        //th1.join();  
        thread_guard t(th1);  
        return 0;  
    }  
    

    线程转移控制权

    #include <iostream>  
    #include <functional>  
    #include <thread>  
    #include <string>  
    #include <stdio.h>  
      
    using namespace std;  
      
    void calc_fun(int n)  
    {  
        int sum=0;  
        for(int i=0;i<n;i++)  
        {  
            sum+=i;  
        }  
        cout<<"calc ret:"<<sum<<endl;  
    }  
      
      
    int main(int argc, char *argv[])  
    {  
        thread th1(calc_fun,10000);  
        thread th2 = std::move(th1);  
        th1.joinable()?th1.join(),1:printf("Th1 can not Join\n");  
        th2.joinable()?th2.join(),1:printf("Th2 can not Join\n");  
        return 0;  
    }  
    

    运行时线程监控

    #include <iostream>  
    #include <algorithm>  
    #include <thread>  
    #include <vector>  
    using namespace std;  
      
    void do_task()  
    {  
        cout<<"Current thread ID:"<<this_thread::get_id()<<endl;  
    }  
      
    int main(int argc,char** argv)  
    {  
        thread::id master_id = this_thread::get_id();  
        //取线程数为cpu核心数的2倍  
        unsigned int  thread_count = thread::hardware_concurrency();  
        vector<std::thread> ths;  
        for(int i=0;i<thread_count;i++)  
        {  
            ths.push_back(std::thread(do_task));  
        }  
      
        for(int i=0;i<thread_count;i++)  
        {  
            ths[i].join();  
        }  
        return 0;  
    }  
    

    相关文章

      网友评论

          本文标题:c++ 11并发之线程运行管理【二】

          本文链接:https://www.haomeiwen.com/subject/wrrvsftx.html