Linux中线程常用接口(创建,等待,退出,取消)

 pthread_create

#include <pthread.h>

       int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);

       Compile and link with -pthread.

编译时应注意。

#include<iostream>
#include<string>
#include<vector>
#include<pthread.h>
#include<unistd.h>

using namespace std;

void *start_routine(void *args)
{
    string name=static_cast<char*>(args);
    while(true)
    {
        cout<<"new thread create success , name: "<<name<<endl;
        sleep(1);
    } 
}

int main()
{
    // vector<pthread_t> tids; 
#define NUM 10
    for(int i=0;i<NUM;i++)
    {
        pthread_t tid;
        char namebuffer[64];
        snprintf(namebuffer,sizeof(namebuffer),"%s:%d","thread",i);
      //pthread_create  
        pthread_create(&tid,nullptr,start_routine,namebuffer);
 //     pthread_create(&tid,nullptr,start_routine,(void*)"thread one");
        sleep(1);
    }
    while(true)
    {
        cout<<"new thread create success,name : main thread"<<endl;
        sleep(1);
    }
    return 0;
}

//可重入函数->void *start_routine(void *args)

//局部变量具有临时性,在多线程中也适用--->每个线程都有自己独立的栈结构

cnt: 9 &cnt   0x7fa036708ef4
cnt: 9 &cnt   0x7fa035f07ef4
cnt: 9 &cnt   0x7fa035706ef4
cnt: 9 &cnt   0x7fa034f05ef4
cnt: 9 &cnt   0x7fa034704ef4
cnt: 9 &cnt   0x7fa033f03ef4
cnt: 9 &cnt   0x7fa033702ef4
cnt: 9 &cnt   0x7fa032f01ef4
cnt: 9 &cnt   0x7fa032700ef4
cnt: 9 &cnt   0x7fa031effef4

void *start_routine(void *args)
{
    sleep(1);
    ThreadData *td=static_cast<ThreadData*>(args);
    int cnt=10;
    while(cnt)
    {
        cout<<"cnt: "<<cnt<<" &cnt   "<<&cnt<<endl;
        cnt--;
        // cout<<"new thread create success , name: "<<td->namebuffer<<" cnt: "<<cnt--<<endl;
        sleep(1);
    } 
    delete td;
    return nullptr;
}
#include<iostream>
#include<string>
#include<vector>
#include<pthread.h>
#include<unistd.h>

using namespace std;
class ThreadData
{
public:
    pthread_t tid;
    char namebuffer[64];
};
//可重入函数
//局部变量具有临时性,在多线程中也适用--->每个线程都有自己独立的栈结构
void *start_routine(void *args)
{
    sleep(1);
    ThreadData *td=static_cast<ThreadData*>(args);
    int cnt=10;
    while(cnt)
    {
        cout<<"cnt: "<<cnt<<" &cnt   "<<&cnt<<endl;
        cnt--;
        // cout<<"new thread create success , name: "<<td->namebuffer<<" cnt: "<<cnt--<<endl;
        sleep(1);
    } 
    delete td;
    return nullptr;
}

int main()
{
    vector<ThreadData*> threads; 

#define NUM 10
    for(int i=0;i<NUM;i++)
    {
        ThreadData *td=new ThreadData(); 
        // char namebuffer[64];
        snprintf(td->namebuffer,sizeof(td->namebuffer),"%s:%d","thread",i+1);
      //pthread_create  
        pthread_create(&td->tid,nullptr,start_routine,td);
 //     pthread_create(&tid,nullptr,start_routine,(void*)"thread one");
        // sleep(1);
        threads.push_back(td);
    }

    for(auto &iter:threads)
    {
        cout<<"create thread: "<<iter -> namebuffer << ":"<<iter->tid<<" success"<<endl;
    }

    while(true)
    {
        cout<<"new thread create success,name : main thread"<<endl;
        sleep(1); 
    }
    return 0;
}

pthread_exit

#include <pthread.h>

       void pthread_exit(void *retval);

       Compile and link with -pthread.

//可重入函数
//局部变量具有临时性,在多线程中也适用--->每个线程都有自己独立的栈结构
void *start_routine(void *args)
{
    sleep(1);
    ThreadData *td=static_cast<ThreadData*>(args);
    int cnt=10;
    while(cnt)
    {
        cout<<"cnt: "<<cnt<<" &cnt   "<<&cnt<<endl;
        cnt--;
        // cout<<"new thread create success , name: "<<td->namebuffer<<" cnt: "<<cnt--<<endl;
        sleep(1);
        pthread_exit(nullptr);//-> return nullptr
        // //任何一个执行流调exit整个进程都会退出
        // exit(0);//不能用来终止线程
    } 
    delete td;
    //return nullptr;//线程函数结束,return的时候线程就算终止了
}

pthread_exit(nullptr);//-> return nullptr

线程等待:pthread_join

       #include <pthread.h>

       int pthread_join(pthread_t thread, void **retval);

       Compile and link with -pthread.
 

线程异常,收到信号,整个进程都会退出。

线程取消: pthread_cancel

       #include <pthread.h>

       int pthread_cancel(pthread_t thread);
       int pthread_cancel(pthread_t thread);

       Compile and link with -pthread.

 线程如果是被取消的,退出码: -1(PTHREAD_CANCELED);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值