(创建于 2018/3/1 上午7:11:44)
查看pthread所有方法
man -k pthread
输出结果
pthread_attr_destroy (3) - initialize and destroy thread attributes object
pthread_attr_getaffinity_np (3) - set/get CPU affinity attribute in thread attributes object
pthread_attr_getdetachstate (3) - set/get detach state attribute in thread attributes object
pthread_attr_getguardsize (3) - set/get guard size attribute in thread attributes object
pthread_attr_getinheritsched (3) - set/get inherit-scheduler attribute in thread attribute...
pthread_attr_getschedparam (3) - set/get scheduling parameter attributes in thread attribu...
pthread_attr_getschedpolicy (3) - set/get scheduling policy attribute in thread attributes...
pthread_attr_getscope (3) - set/get contention scope attribute in thread attributes object
pthread_attr_getstack (3) - set/get stack attributes in thread attributes object
pthread_attr_getstackaddr (3) - set/get stack address attribute in thread attributes object
pthread_attr_getstacksize (3) - set/get stack size attribute in thread attributes object
pthread_attr_init (3) - initialize and destroy thread attributes object
pthread_attr_setaffinity_np (3) - set/get CPU affinity attribute in thread attributes object
pthread_attr_setdetachstate (3) - set/get detach state attribute in thread attributes object
pthread_attr_setguardsize (3) - set/get guard size attribute in thread attributes object
pthread_attr_setinheritsched (3) - set/get inherit-scheduler attribute in thread attribute...
pthread_attr_setschedparam (3) - set/get scheduling parameter attributes in thread attribu...
pthread_attr_setschedpolicy (3) - set/get scheduling policy attribute in thread attributes...
pthread_attr_setscope (3) - set/get contention scope attribute in thread attributes object
pthread_attr_setstack (3) - set/get stack attributes in thread attributes object
pthread_attr_setstackaddr (3) - set/get stack address attribute in thread attributes object
pthread_attr_setstacksize (3) - set/get stack size attribute in thread attributes object
pthread_cancel (3) - send a cancellation request to a thread
pthread_cleanup_pop (3) - push and pop thread cancellation clean-up handlers
pthread_cleanup_pop_restore_np (3) - push and pop thread cancellation clean-up handlers wh...
pthread_cleanup_push (3) - push and pop thread cancellation clean-up handlers
pthread_cleanup_push_defer_np (3) - push and pop thread cancellation clean-up handlers whi...
pthread_create (3) - create a new thread
pthread_detach (3) - detach a thread
pthread_equal (3) - compare thread IDs
pthread_exit (3) - terminate calling thread
pthread_getaffinity_np (3) - set/get CPU affinity of a thread
pthread_getattr_np (3) - get attributes of created thread
pthread_getconcurrency (3) - set/get the concurrency level
pthread_getcpuclockid (3) - retrieve ID of a thread's CPU time clock
pthread_getname_np (3) - set/get the name of a thread
pthread_getschedparam (3) - set/get scheduling policy and parameters of a thread
pthread_join (3) - join with a terminated thread
pthread_kill (3) - send a signal to a thread
pthread_kill_other_threads_np (3) - terminate all other threads in process
pthread_rwlockattr_getkind_np (3) - set/get the read-write lock kind of the thread read-wr...
pthread_rwlockattr_setkind_np (3) - set/get the read-write lock kind of the thread read-wr...
pthread_self (3) - obtain ID of the calling thread
pthread_setaffinity_np (3) - set/get CPU affinity of a thread
pthread_setcancelstate (3) - set cancelability state and type
pthread_setcanceltype (3) - set cancelability state and type
pthread_setconcurrency (3) - set/get the concurrency level
pthread_setname_np (3) - set/get the name of a thread
pthread_setschedparam (3) - set/get scheduling policy and parameters of a thread
pthread_setschedprio (3) - set scheduling priority of a thread
pthread_sigmask (3) - examine and change mask of blocked signals
pthread_sigqueue (3) - queue a signal and data to a thread
pthread_testcancel (3) - request delivery of any pending cancellation request
pthread_timedjoin_np (3) - try to join with a terminated thread
pthread_tryjoin_np (3) - try to join with a terminated thread
pthread_yield (3) - yield the processor
pthreads (7) - POSIX threads
root@iZbp11v3y27wpf6mglp2glZ:/user/renzhenming/pthread#
1.线程创建与退出
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
void* thr_fun(void* arg){
char* no = (char*)arg;
int i = 0;
for(; i < 10; i++){
printf("%s thread, i:%d\n",no,i);
if(i==5){
//线程退出(自杀)
pthread_exit(2);
//他杀pthread_cancel
}
}
return 1;
}
void main(){
printf("main thread\n");
//线程id
pthread_t tid;
//线程的属性,NULL默认属性
//thr_fun,线程创建之后执行的函数
pthread_create(&tid,NULL,thr_fun,"1");
void* rval;
//等待tid线程结束
//thr_fun与退出时传入的参数,都作为第二个参数的内容
pthread_join(tid,&rval);
printf("rval:%d\n",(int)rval);
}
2.互斥锁
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
int i = 0;
//互斥锁
pthread_mutex_t mutex;
void* thr_fun(void* arg){
//加锁
pthread_mutex_lock(&mutex);
char* no = (char*)arg;
for(;i < 5; i++){
printf("%s thread, i:%d\n",no,i);
sleep(1);
}
i=0;
//解锁
pthread_mutex_unlock(&mutex);
}
void main(){
pthread_t tid1, tid2;
//初始化互斥锁
pthread_mutex_init(&mutex,NULL);
pthread_create(&tid1,NULL,thr_fun,"No1");
pthread_create(&tid2,NULL,thr_fun,"No2");
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
//销毁互斥锁
pthread_mutex_destroy(&mutex);
}
3.线程唤醒与等待
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
//消费者数量
#define CONSUMER_NUM 2
//生产者数量
#define PRODUCER_NUM 1
pthread_t pids[CONSUMER_NUM+PRODUCER_NUM];
//产品队列
int ready = 0;
//互斥锁
pthread_mutex_t mutex;
//条件变量
pthread_cond_t has_product;
//生产
void* producer(void* arg){
int no = (int)arg;
//条件变量
for(;;){
pthread_mutex_lock(&mutex);
//往队列中添加产品
ready++;
printf("producer %d, produce product\n",no);
//fflush(NULL);
//通知消费者,有新的产品可以消费了
//pthread_cond_signal会阻塞输出,假设发出信号后没有接收信号的线程,
//会导致上边一行的打印不会出现,被它阻塞了,输出还在缓冲区中没有打印出来,刷新缓冲区fflush后可以显示
pthread_cond_signal(&has_product);
printf("producer %d, singal\n",no);
pthread_mutex_unlock(&mutex);
sleep(1);
}
}
//消费者
void* consumer(void* arg){
int num = (int)arg;
for(;;){
pthread_mutex_lock(&mutex);
//while?(为什么不用if)
//superious wake ‘惊群效应’
//因为线程被唤醒的条件不只时我们发送signal,还有一个经群效应导致的线程唤醒,如果用if,会导致这种情况下的问题,用while可以持续的判断条件ready==0,避免这种情况
while(ready==0){
//没有产品,继续等待
//1.阻塞等待has_product被唤醒
//2.释放互斥锁,pthread_mutex_unlock
//3.被唤醒时,解除阻塞,重新申请获得互斥锁pthread_mutex_lock
printf("%d consumer wait\n",num);
pthread_cond_wait(&has_product,&mutex);
}
//有产品,消费产品
ready--;
printf("%d consume product\n",num);
pthread_mutex_unlock(&mutex);
sleep(1);
}
}
void main(){
//初始化互斥锁和条件变量
pthread_mutex_init(&mutex,NULL);
pthread_cond_init(&has_product,NULL);
printf("init\n");
int i;
for(i=0; i<PRODUCER_NUM;i++){
//生产者线程
printf("%d\n",i);
pthread_create(&pids[i],NULL,producer,(void*)i);
}
for(i=0; i<CONSUMER_NUM;i++){
//消费者线程
pthread_create(&pids[PRODUCER_NUM+i],NULL,consumer,(void*)i);
}
//等待
sleep(10);
for(i=0; i<PRODUCER_NUM+CONSUMER_NUM;i++){
pthread_join(pids[i],NULL);
}
//销毁互斥锁和条件变量
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&has_product);
}
//while?(为什么不用if)