Linux 多线程编程

线程管理与同步

1.创建线程和退出的函数原型 :

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

pthread_exit(0);

其他还有很多相关的函数。

2.编译时要加上: -lpthread ;在要包含的头文件中加上   #include<pthread.h>。

3.暂停线程:

问题:在主进程中调用sleep(), 使整个进程包括它的线程都挂起了,所以不能完成同步的任务。

int pthread_jion(pthread_t  th, void* * thread_return );

 4线程同步:

互斥锁(mutex), 条件变量(condition vriaible)

pthread_mutex_init( )

pthread_mutex_destory()

pthread_mutex_lock()

pthread_mutex_trylock()

pthread_mutex_unlock()

处理条件变量的一些函数:

pthread_cond_init()

pthread_cond_destroy()

pthread_cond_wait()

pthread_cond_timedwait()

pthread_cond_signal()

pthread_cond_broadcast()

5.线程终止

int pthread_cancel()

int pthread_setcancelstate()

int pthread_setcanceltype()

void pthread_testcancel()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值