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

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

 

注:上述的多线程,在向容器vector压如数据的时候,一定要加锁,否则会导致段错误,这个是血的教训.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值