join.c
关键词:pthread_join()
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define NUM_THREADS 4
void *BusyWork(void *t)
{
int i;
long tid;
double result=0.0;
tid = (long)t;
printf("Thread %ld starting...\n",tid);
for (i=0; i<1000000; i++)
{
result = result + sin(i) * tan(i);
}
printf("Thread %ld done. Result = %e\n",tid, result);
pthread_exit((void*) t);
}
int main (int argc, char *argv[])
{
pthread_t thread[NUM_THREADS];
pthread_attr_t attr;
int rc;
long t;
void *status;
/* Initialize and set thread detached attribute */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for(t=0; t<NUM_THREADS; t++) {
printf("Main: creating thread %ld\n", t);
rc = pthread_create(&thread[t], &attr, BusyWork, (void *)t);
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
/* Free attribute and wait for the other threads */
pthread_attr_destroy(&attr);
for(t=0; t<NUM_THREADS; t++) {
rc = pthread_join(thread[t], &status);
if (rc) {
printf("ERROR; return code from pthread_join() is %d\n", rc);
exit(-1);
}
printf("Main: completed join with thread %ld having a status of %ld\n",t,(long)status);
}
printf("Main: program completed. Exiting.\n");
pthread_exit(NULL);
}
代码输出如下:
Main: creating thread 0
Main: creating thread 1
Thread 0 starting...
Main: creating thread 2
Thread 1 starting...
Main: creating thread 3
Thread 2 starting...
Thread 3 starting...
Thread 1 done. Result = -3.153838e+06
Thread 2 done. Result = -3.153838e+06
Thread 0 done. Result = -3.153838e+06
Main: completed join with thread 0 having a status of 0
Main: completed join with thread 1 having a status of 1
Main: completed join with thread 2 having a status of 2
Thread 3 done. Result = -3.153838e+06
Main: completed join with thread 3 having a status of 3
Main: program completed. Exiting.
注意:
- 通过显式设置线程属性中的参数来确保创建的线程能够被连接,这使得代码具有更高的可移植性和可靠性
- 主线程通过调用pthread_join()等待子线程结束执行,并接收了子线程中pthread_exit()传递的参数
如果我们仿照hello_args.c中的做法,将BusyWork()中pthread_exit()的入参由(void*)t改为(void*)&t,同时将main()中的打印传参由(long)status改为*(long*)status,会发现打印出的status值是错误的。这是一种错误的传参,因为此时我们尝试通过地址访问子线程退出时已经销毁了的堆栈信息。
detach.c
关键词:线程分离
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define NUM_THREADS 4
void *BusyWork(void *t)
{
long i, tid;
double result=0.0;
tid = (long)t;
printf("Thread %ld starting...\n",tid);
for (i=0; i<1000000; i++) {
result = result + sin(i) * tan(i);
}
printf("Thread %ld done. Result = %e\n",tid, result);
}
int main(int argc, char *argv[])
{
pthread_t thread[NUM_THREADS];
pthread_attr_t attr;
int rc;
long t;
/* Initialize and set thread detached attribute */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
for(t=0;t<NUM_THREADS;t++) {
printf("Main: creating thread %ld\n", t);
rc = pthread_create(&thread[t], &attr, BusyWork, (void *)t);
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
/* We're done with the attribute object, so we can destroy it */
pthread_attr_destroy(&attr);
/* The main thread is done, so we need to call pthread_exit explicitly to
* permit the working threads to continue even after main completes.
*/
printf("Main: program completed. Exiting.\n");
pthread_exit(NULL);
}
相比join.c,这里显示创建了分离状态的子线程,因此主线程也没有调用pthread_join()来连接子线程;实际上此时如果调用pthread_join()会返回非0值,表示调用失败。
一般情况下我们会创建可连接的子线程,配合pthread_join()使用;如果子线程的执行和主线程在逻辑上没有关联和依赖,可以考虑显示创建分离状态的线程,这可以减少一些系统资源占用。