乡 曙光 计算,改变您的现在 DAWNING pthread_ join(pthread _t wait_for, void** status); 等待直到线程结束 执行该函数的线程发生阻塞,直到由 wait for指定的线程终止 等与被等的两线程必须是同一进程内部的线程(而且不是分离线 程 返回值 成功返回 ESRCH参数 wait for指定的线程不存在或是一分离线 程 EINVAL线程参数无效; EDEADLK等待自身结束。 不能有两个线程同时等待同一个线程的结束,否则其中一个线程 正常返回另外一个返回 ESRCH错误 2006年4月 共享存储编程 16/108
2006年4月 共享存储编程 16/108 pthread_join(pthread_t wait_for,void** status); 等待直到线程结束; 执行该函数的线程发生阻塞,直到由wait_for指定的线程终止; 等与被等的两线程必须是同一进程内部的线程(而且不是分离线 程); 返回值 0 成功返回 ESRCH 参数wait_for指定的线程不存在或是一分离线 程; EINVAL 线程参数无效; EDEADLK 等待自身结束. 不能有两个线程同时等待同一个线程的结束, 否则其中一个线程 正常返回,另外一个返回ESRCH错误
乡 曙光 计算,改变您的现在 DAWNING Hello world(2 Include <pthread.h> include"stdio. h Hello world from thread o! define numthrds 5 Hello World from thread 1! pthread_t"tid; Hello World from thread 2! void*worker(; Hello World from thread 3 main0【 Hello world from thread 4! int i tid =(pthread_t)calloc(numthrds, sizeof(pthread_tD); fori=oji<numthrds;i++) pthread_create (&tid[I, NULL,worker, NULLE fori=Oji<numthrds; i++) pthread _join(tid[],NULL; void* worker(0【 int myid; myid s pthread_selfo- tid[oi printf( Hello World from thread %dhn",myid); 2006年4月 共享存储编程 17/108
2006年4月 共享存储编程 17/108 Hello World(2) #include <pthread.h> #include "stdio.h" #define numthrds 5 pthread_t *tid; void *worker(); main(){ int i; tid = (pthread_t*) calloc(numthrds,sizeof(pthread_t)); for(i=0;i<numthrds;i++) pthread_create(&tid[i],NULL,worker,NULL); for(i=0;i<numthrds;i++) pthread_join(tid[i],NULL); } void *worker(){ int myid; myid = pthread_self() - tid[0]; printf("Hello World from thread %d!\n",myid); } Hello World from thread 0! Hello World from thread 1! Hello World from thread 2! Hello World from thread 3! Hello World from thread 4!
乡 曙光 计算,改变您的现在 DAWNING Hello World (3) #include <pthread.h> void*worker(t include h int myid, num; idefine numthrds 5 myid s pthread_ selfo-[o printf("%d was added to the pthread_t"tid sum in thread %d\n", myid*10, myid); pthread_mutex_t mutex;i pthread_mutex_lock (&mutex; int sun〓0 sum + myid*10; void *worker OF pthread_mutex_unlock(&mutex); main(t returns int is tid = (pthreadt*)calloc(numthrds, sizeof(pthread_t; pthread_mutex_init (&mutex, NULLE for=0;i<numthrds;i++) pthread_create(&tid[, NULL, worker, NULL; for(i=0;i<numthrds;i++) pthread_ _join(tid[,NULL); printf("The sum is %dIn"sum); 2006年4月 共享存储编程 18/108
2006年4月 共享存储编程 18/108 Hello World(3) #include <pthread.h> #include "stdio.h" #define numthrds 5 pthread_t *tid; pthread_mutex_t mutex; int sum=0; void *worker(); main(){ int i; tid = (pthread_t*) calloc(numthrds,sizeof(pthread_t)); pthread_mutex_init(&mutex,NULL); for(i=0;i<numthrds;i++) pthread_create(&tid[i],NULL,worker,NULL); for(i=0;i<numthrds;i++) pthread_join(tid[i],NULL); printf(“The sum is %d\n",sum); } void *worker(){ int myid,num; myid = pthread_self() - tid[0]; printf("%d was added to the sum in thread %d\n",myid*10,myid); pthread_mutex_lock(&mutex); sum += myid*10; pthread_mutex_unlock(&mutex); return; }
乡 曙光 计算,改变您的现在 DAWNING 运行结果 0 was added to the sum in thread o 10 was added to the sum in thread 1 20 was added to the sum in thread 2 30 was added to the sum in thread 3 40 was added to the sum in thread 4 The sum is 100 2006年4月 共享存储编程 19/108
2006年4月 共享存储编程 19/108 运行结果 0 was added to the sum in thread 0 10 was added to the sum in thread 1 20 was added to the sum in thread 2 30 was added to the sum in thread 3 40 was added to the sum in thread 4 The sum is 100
乡 曙光 计算,改变您的现在 DAWNING 基于多线程编程的P求解 2 x 2006年4月 共享存储编程 20/108
2006年4月 共享存储编程 20/108 基于多线程编程的PI求解 4 1 d 1 0 2 − = x x 2 2