POSIX多线程编程(二)--pthread_join

本文详细探讨了POSIX多线程编程中的pthread_join函数,阐述了如何声明并初始化pthread_attr_t类型变量,设置线程属性的分离状态,以及在完成时如何释放相关资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

To explicitly create a thread as joinable or detached, the attr argument in the pthread_create() routine is used. The typical 4 step process is:
  1. Declare a pthread attribute variable of the pthread_attr_t data type
  2. Initialize the attribute variable with pthread_attr_init()
  3. Set the attribute detached status with pthread_attr_setdetachstate()
  4. When done, free library resources used by the attribute with pthread_attr_destroy()

/**
 * NAME
 * pthread_join - wait for thread termination

 * SYNOPSIS
 * #include <pthread.h>

 * int pthread_join(pthread_t thread, void **value_ptr);


 * DESCRIPTION
 * The  pthread_join()  function  shall  suspend  execution of the calling
 * thread until the target thread terminates, unless the target thread has
 * already  terminated.  On  return  from a successful pthread_join() call
 * with a non-NULL value_ptr argument, the value passed to  pthread_exit()
 * by  the terminating thread shall be made available in the location ref-
 * erenced by value_ptr. When a pthread_join() returns  successfully,  the
 * target thread has been terminated. The results of multiple simultaneous
 * calls to pthread_join() specifying the same  target  thread  are  unde-
 * fined.  If the thread calling pthread_join() is canceled, then the tar-
 * get thread shall not be detached.

 * It is unspecified whether a thread that has exited but remains unjoined
 * counts against {PTHREAD_THREADS_MAX}.

 * RETURN VALUE
 * If  successful,  the  pthread_join() function shall return zero; other-
 * wise, an error number shall be returned to indicate the error.
 */
#include <pthread.h>
#include <stdio.h>
#include <stdlib.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);
	return NULL ;
}

int main(int argc, char *argv[])
{
	pthread_t thread[NUM_THREADS];

	//定义pthread_attr_t变量
	pthread_attr_t attr;
	int rc;
	long t;
	void *status;

	/* Initialize and set thread detached attribute */
	/**
	 * 主要注意一下两个方法
	 * 初始化attr
	 * pthread_attr_init
	 * 设置这个线程是可以joinable
	 * pthread_attr_setdetachstate
	 */
	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);
		//主要是函数的第二个参数
		//把设置好的attr传入
		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 */
	//释放attribute
	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 );
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值