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

注意,原本的函数操作时间不能太短,否则多线程没有效率优势,因为在切换线程的时候是需要有时间成本的。

猜你喜欢

转载自blog.csdn.net/qq_31638535/article/details/89517620