linux 多线程的学习

本文详细介绍了pthread库中的几个关键函数:pthread_exit用于从线程退出并返回一个值;pthread_join用于等待一个线程结束并获取其返回值;pthread_cancel用于取消一个正在运行的线程;而pthread_detach则用于分离线程,使主线程不必等待该线程结束。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pthread_exit的一个目标是,把一个指针传递给pthread_join函数
pthread_join函数的思路是:通过参数的返回值,将该指针值返回给pthread_join的调用者
#include<pthread.h>
#include<iostream>
#include<unistd.h>

using namespace std;
void* thread(void* arg){
 cout<<(long)arg<<endl;
  return (void*)20;
}
int main(){
  pthread_t tid;
  int err=pthread_create(&tid,NULL,thread,(void*)2);
  if(err!=0){
    cout<<"create failure"<<endl;
    return -1;
  }
  long* r;
  pthread_join(tid,(void**)&r);
  cout<<(long)r<<endl;
  return 0;
}
编译:g++ tt.cc -lpthread

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

using namespace std;

void *thread(void *arg)
{
    cout << "in thread, tid = " << pthread_self() << endl;

    long i = 2;
    pthread_exit(&i);

    return (void *)12;
}

int main()
{
    pthread_t tid;
    if(pthread_create(&tid, NULL, thread, 0) != 0)
    {
        cout << "pthread_create error" << endl;
        return 0;
    }

    long *r;
    pthread_join(tid, (void**)&r);

    cout << r << endl;

    cout << *r << endl;

    cout << "in main thread, tid = " << pthread_self() << endl;
    return 0;
}
上述代码中,*r指向了一个不再存在的对象,输出结果为:
in thread, tid = 139814095632128
0x7f2901812e88
139814095632128
in main thread, tid = 139814112380736

long i = 2;
    pthread_exit(&i);
改为pthread_exit(new long(2));
in thread, tid = 140514155718400
0x7fcbf80008c0
2
in main thread, tid = 140514172467008

三:pthread_cancel的使用:
#include<pthread.h>
#include<iostream>
#include<unistd.h>

using namespace std;

void *thread(void *arg)
{
    cout << "in thread, tid = " << pthread_self() << endl;

    sleep(2);

    return (void *)12;
}

int main()
{
    pthread_t tid;
    if(pthread_create(&tid, NULL, thread, 0) != 0)
    {
        cout << "pthread_create error" << endl;
        return 0;
    }
    pthread_cancel(tid);

    long *r;
    pthread_join(tid, (void**)&r);

    cout << PTHREAD_CANCELED << endl;
    cout << r << endl;

    cout << "in main thread, tid = " << pthread_self() << endl;
    return 0;
}
执行结果:
in thread, tid = 140456113821440

四:pthread_detach的使用:
pthread_joinpthread_detach先调用,也能获取到退出信息
#include<pthread.h>
#include<iostream>
#include<unistd.h>
#include<errno.h>

using namespace std;

void *thread(void *arg)
{
    cout << "in thread, tid = " << pthread_self() << endl;

    sleep(2);

    pthread_detach(pthread_self());

    cout << "nihao" << endl;

    return (void *)0;
}

int main()
{
    pthread_t tid;
    if(pthread_create(&tid, NULL, thread, 0) != 0)
    {
        cout << "pthread_create error" << endl;
        return 0;
    }

    int *r;
    int s = pthread_join(tid, (void **)&r);
    if(s == EINVAL)
    {
        cout << "join error" << endl;
    }
    else
    {
        cout << r << endl;
    }

    cout << "in main thread, tid = " << pthread_self() << endl;

    return 0;
}
运行结果:
in thread, tid = 140355871602432
nihao
0
in main thread, tid = 140355888351040




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值