C++ 多線程入門(C++11)

多線程併發一直是我想了解的東西,現在也算入門了,寫一下我的總結。

這是我寫的一個簡單的code,下面貼一下運行結果,看完大致入門多線程:

#include <thread>

#include <iostream>

#include <vector>

#include<unistd.h>
using namespace std;
class Calculation{

public:
    explicit Calculation(long a):num(a){};
    ~Calculation(){};

    inline void add1(){
        for(int i =0; i<1000;i++)
        {
            num++;
            usleep(1000);
        }


    };
    inline void add10(){
        for(int i =0; i<1000;i++)
        {
//            num++;
            num+=10;
             usleep(1000);
        }
    };
    inline void add100(){
        for(int i =0; i<1000;i++)
        {
//            num++;
            num+=100;
             usleep(1000);
        }

    };
    inline void add1000(){
        for(int i =0; i<1000;i++)
        {
//            num++;
            num+=1000;
             usleep(1000);
        }
    };
    inline long GetNum(){return num;};

private:
    long num;

};


int main()

{
    std::vector<std::thread> threads;
    bool ifMulti = false;


//    for(int i = 0; i < 5; ++i){
if(!ifMulti)
{
    Calculation test1(1);
    Calculation test10(1);
    Calculation test100(1);
    Calculation test1000(1);

    std::chrono::high_resolution_clock::time_point tnow = std::chrono::high_resolution_clock::now();
    test1.add1();
                   cout <<"add 1 step :"  <<test1.GetNum()<<endl;
    test10.add10();
                   cout <<"add 10 step :"  <<test10.GetNum()<<endl;
    test100.add100();
                   cout <<"add 100 step :"  <<test100.GetNum()<<endl;
    test1000.add1000();
                   cout <<"add 1000 step :"  <<test1000.GetNum()<<endl;



    std::chrono::high_resolution_clock::time_point tpost = std::chrono::high_resolution_clock::now();
    std::cout << "no multi cost time: " << std::chrono::duration_cast<std::chrono::duration<double>>(tpost - tnow).count() * 1000 << " ms" << std::endl;
}



if(ifMulti)
{
    std::chrono::high_resolution_clock::time_point tnow = std::chrono::high_resolution_clock::now();

        threads.push_back(std::thread(
                              [&](){
            Calculation test1(1);

               test1.add1();
               cout <<"add 1 step :"  <<test1.GetNum()<<endl;
        }
));

        threads.push_back(std::thread(
                              [&](){
            Calculation test10(1);


               test10.add10();
               cout <<"add 10 step :" <<test10.GetNum()<<endl;
        }
        ));
        threads.push_back(std::thread(
                              [&](){

            Calculation test100(1);

               test100.add100();
               cout <<"add 100 step :"  <<test100.GetNum()<<endl;
        }
        ));
        threads.push_back(std::thread(
                              [&](){

            Calculation test1000(1);
               test1000.add1000();
               cout << "add 1000 step :"<<test1000.GetNum()<<endl;
        }
        ));
//    }
    for(auto& thread : threads){

        thread.join();
    }

//    std::cout << test1.GetNum() <<std::endl;
//    std::cout<<"Main Thread"<<"\t"<<std::this_thread::get_id()<<std::endl;
    std::chrono::high_resolution_clock::time_point tpost = std::chrono::high_resolution_clock::now();
    std::cout << "muti_threads cost time: " << std::chrono::duration_cast<std::chrono::duration<double>>(tpost - tnow).count() * 1000 << " ms" << std::endl;
}
    return 0;
}

當ifMulti 爲false的時候,依次運行完四個對象的操作,運行結果如下:

add 1 step :1001
add 10 step :10001
add 100 step :100001
add 1000 step :1000001
no multi cost time: 4662.88 ms

當ifMulti 爲true的時候,並行運行完四個對象的操作,運行結果如下:

add 1 step :1001
add 10 step :10001
add 1000 step :1000001
add 100 step :100001
muti_threads cost time: 1146.31 ms

注意,原本的函數操作時間不能太短,否則多線程沒有效率優勢,因爲在切換線程的時候是需要有時間成本的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章