CPP 学习笔记-多线程

知识点1
  • Linux 下编译 带<thread> 的CPP需要加上 -pthread 编译选项。例如: ``` g++ -std=c++11 -pthread a.cpp
  • <li>QT Creator 编译带&ltthread&gt的CPP有BUG,暂时不知道怎么去除!
    
    ####  代码1
    
    ```#include <iostream>
    #include <stdlib.h>v
    #include <thread>
    
    using namespace std;
    void run(char* p)
    {
        int i=0;
        i=system(p);
    }
    
    int main()
    {
        char p[5][20]={
            "ls",
            "echo nihao",
            "gnome-terminal",
            "terminator",
            "ps -aux"
    
        };
    
        while("nimei")
        {
           static  int i(0);
            if(i<5){
                thread  *pt=new  thread(run,p[i]);
                i+=1;
                cout<<"I now is :\t"<<i<<endl;
            }
            else{
                break;
            }
    
            cout<<"Breaking...."<<endl;
        }
    
    
    
    
        cin.get();
        return 0;
    }
    
    知识点2
  • 关于thread类的内部成
  • 关键字详细解释
    idThread的id
    native_handle_typenative_handle_type
    operator=Move massive Thread
    get_idget Thread ID
    joinableget if joinable
    joinjoin thread
    detachdetach thread
    swapswap thread
    native_handleget native handle
    hardware_concurrency[static]Detect hardware concurrency (public static function)

    1.jpeg

  • 线程 detach 脱离主线程的绑定,主线程挂了,子线程不报错,子线程执行完自动退出。
  • 线程 detach以后,子线程会成为孤儿线程,线程之间将无法通信。
  • 知识点3

  • 线程中变量的竞争控制是通过 mutex automic 来实现的
  • mutex : 互斥量。需要包含头文件 <mutex> 来使用 -->速度慢
  • atomic 原子变量。需要包含头文件<atomic>来实现 -->速度快,线程安全。
  • 代码3
    #include <iostream>
    #include <stdlib.h>
    #include <thread>
    #include <atomic>
    
    using namespace std;
    int count(0);
    
    void run()
    {
        for(int i(0);i<1000000;i++)
        {
            count++;
            cout<<"\t"<<i<<"\t"<<count<<"\t";
        }
    }
    
    int main()
    {
        auto n=thread::hardware_concurrency();
    
        thread* pt[n];
        for(int z=0;z<n;z++)
        {
            pt[z]=new thread(run);
            pt[z]->detach();
    
        }
    
        cout<<"Finally count is \t"<<count<<endl;
        cout<<"Used "<<n <<"threads"<<endl;
    
    
    
    
        cin.get();
        return 0;
    }
    

    运行结果不是1000000×2.

    #include <stdlib.h>
    #include <thread>
    #include <atomic>
    
    using namespace std;
    int count(0);
    
    void run()
    {
        for(int i(0);i<1000000;i++)
        {
            count++;
            cout<<"\t"<<i<<"\t"<<count<<"\t";
        }
    }
    
    int main()
    {
        auto n=thread::hardware_concurrency();
    
        thread* pt[n];
        for(int z=0;z<n;z++)
        {
            pt[z]=new thread(run);
            pt[z]->detach();
    
        }
    
        cout<<"Finally count is \t"<<count<<endl;
        cout<<"Used "<<n <<"threads"<<endl;
    
    
    
    
        cin.get();
        return 0;
    }
    

    运行结果是1000000×2.正确

  • atomic 声明方式为 atomic<int> a(100); 等号初始化会报错
  • vim 按CTRL+S 后假死按 CTRL+q 退出
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值