Linux线程基本编程

pthread_create

在调用这个函数的进程中创建函数原型

#include <pthread.h>
int pthread_create(pthread_t *thread, 
                   const pthread_attr_t *attr,
                   void *(*start_routine) (void *), 
                   void *arg);

参数

thread

属于结果参数。函数结束时,返回线程ID并存储到thread中。
pthread_t被定义成unsigned long int类型。

attr

设置线程的属性,主要是栈相关的属性。

start_routine

start_routine是一个回调函数(函数指针实现)。指明了线程要执行的函数。

arg

回调函数start_routine()执行时的参数(实参)。

返回值

成返回0,失败返回非0值。一个新的线程

pthread_exit

结束一个线程

函数原型

#include <pthread.h>
void pthread_exit(void*retval);

参数

retval用来保存线程退出状态

返回值

为空。因为该函数永远成功

pthread_join

为了回收资源,主线程会等待子线程结束。该函数就是用来等待线程终止的。类似与进程中的wait函数。

此函数将阻塞调用当前线程的进程,直到此线程退出。

函数原型

#include <pthread.h>
int pthread_join(pthread_t thread, void **retval);

参数

thread

被等待线程的ID

retval

如果此值非NULL,pthread_join复制线程的退出状态

返回值

成功返回0,失败返回非0

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值