C++的线程

复制代码
#include<iostream>
#include<thread>
#include<unistd.h>
using namespace std;
void myrun()
{
    while(true)
    {
    cout<<"I am a thread"<<endl;
    sleep(1);
    }
}
int main()
{
    thread t(myrun);
    t.join();
    return 0;
}

如果不添加-lpthread就会报错。

用纯c++的接口创建线程时,在Linux中还是要包含线程库。

这说明c++的那些库本质就是pthread原生线程库的封装。

这样,我们学习别的语言,只需要学习接口,因为我们知道底层是一样的。

说到底,这样的设计就是为了跨平台性。

接下来了解一下线程的局部存储。

__thread可以给每个执行流一个变量。

在编译链接库时,就会给每一个变量在线程局部存储中开辟空间。

cpp 复制代码
#include<iostream>
#include<thread>
#include<unistd.h>
using namespace std;
__thread int g_val=100;
void myrun()
{
    while(g_val--)
    {
    cout<<"I am a new thread"<<"       "<<g_val<<"      "<<&g_val<<endl;
    sleep(1);
    }
}
int main()
{
    thread t(myrun);
    while(g_val--)
    {
    cout<<"I am main thread"<<"       "<<g_val<<"      "<<&g_val<<endl;
    sleep(2);
    }
    t.join();
    return 0;
}

下面是多线程的一个简单模版。

cpp 复制代码
//test.hpp
#include<iostream>
#include<thread>
#include<unistd.h>
#include<functional>
#include<vector>
using namespace std;
template<class T>
using func_t=function<void(T)>;//返回值为void,参数为T
template<class T>
class Thread
{
    public:
    Thread(func_t<T> func,const string&name,T data)
    :_tid(0)
    ,_func(func)
    ,_threadname(name)
    ,isrunning(false)
    ,_data(data)
    {
    }
    static void*ThreadRoutine(void*args)
    {
        //(void)args;//仅仅是为了防止编译器有告警
        Thread*ts=static_cast<Thread*>(args);
        ts->_func(ts->_data);
        return nullptr;
    }
    bool Start()
    {
        int n=pthread_create(&_tid,nullptr,ThreadRoutine,this);
        if(n==0)
        {
        isrunning=true;
         return true;
        }
        return false;
    }
    bool Join()
    {
        if(!isrunning) return true;
        int n=pthread_join(_tid,nullptr);
        if(n==0)
        {
            isrunning=false;
            return true;
        }
        return false;

    }
    string GetThreadName()
    {
        return _threadname;
    }
    bool IsRunning()
    {
        return isrunning;
    }
    ~Thread()
    {}
    private:
    pthread_t _tid;
    string _threadname;
    bool isrunning;
    func_t<T> _func;
    T _data;
};
cpp 复制代码
#include"test.hpp"
using namespace std;
string GetThreadName()
{
    static int number=1;
    char name[64];
    snprintf(name,sizeof name,"Thread - %d",number++);
    return name;
}
void print(int num)
{
    while(num--)
    {
    cout<<"hello world"<<num<<endl;
    sleep(1);
    }
}
int main()
{
    int num=5;
    vector<Thread<int>> Threads;
    while(num--)
    {
        Threads.push_back(Thread<int>(print,GetThreadName(),10));
    }
    for(auto &t:Threads)
    {
        cout<<t.GetThreadName()<<" is running? "<<t.IsRunning()<<endl;
    }
    for(auto&t:Threads)
    {
        t.Start();
    }
    for(auto &t:Threads)
    {
        cout<<t.GetThreadName()<<" is running? "<<t.IsRunning()<<endl;
    }
    for(auto &t:Threads)
    {
        t.Join();
    }
    // Thread t(print,GetThreadName());
    // cout<<"Is thread running?"<<t.IsRunning()<<endl;
    // t.Start();
    // cout<<"Is thread running?"<<t.IsRunning()<<endl;
    // t.Join();
    return 0;
}
相关推荐
belldeep13 分钟前
如何阅读、学习 Tcc (Tiny C Compiler) 源代码?如何解析 Tcc 源代码?
c语言·开发语言
LuckyTHP14 分钟前
java 使用zxing生成条形码(可自定义文字位置、边框样式)
java·开发语言·python
Trent19851 小时前
影楼精修-肤色统一算法解析
图像处理·人工智能·算法·计算机视觉
feifeigo1231 小时前
高光谱遥感图像处理之数据分类的fcm算法
图像处理·算法·分类
a东方青2 小时前
蓝桥杯 2024 C++国 B最小字符串
c++·职场和发展·蓝桥杯
北上ing2 小时前
算法练习:19.JZ29 顺时针打印矩阵
算法·leetcode·矩阵
.格子衫.3 小时前
真题卷001——算法备赛
算法
XiaoyaoCarter3 小时前
每日一道leetcode
c++·算法·leetcode·职场和发展·二分查找·深度优先·前缀树
Blossom.1183 小时前
使用Python实现简单的人工智能聊天机器人
开发语言·人工智能·python·低代码·数据挖掘·机器人·云计算
da-peng-song3 小时前
ArcGIS Desktop使用入门(二)常用工具条——数据框工具(旋转视图)
开发语言·javascript·arcgis