线程篇POSIX线程(第二讲)

POSIX线程库

 

 pthread_create函数

 

错误检查 

 

pthread_exit函数 

 

举个栗子:

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define ERR_EXIT(m)				\
		do						\
		{						\
			perror(m);			\
			exit(EXIT_TAILURE);	\
		}while(0)
void* thread_routine(void* arg)
{//这个函数是创建线程所执行的函数,函数名称是
//创建线程函数的参数。这个函数的参数是也是由
//创建线程函数传递过来的
//线程执行是交替执行的
	for(int i = 0;i < 20; ++i)
	{
		printf("B");
		fflush(stdout);
	}
	return 0;
}
int main()
{
	pthread_t tid;
	int ret;

	if((ret = pthread_create(&tid,NULL,thread_routine,NULL)) != 0)
	{//创建线程的函数,成功返回0,失败返回错误码,所以if语句中
	 //可以直接打印错误码。
	//第一个参数是线程ID号,第二个参数是NULL,一般为空就可以了,
	//第三个参数是线程执行的函数,第四个参数是给线程传递的参数
		fprintf(stderr,"pthread_create:%s\n",strerror(ret));
		exit(EXIT_FAILURE);
	}

	for(int i = 0; i < 20;++i)
	{
		printf("A");
		fflush(stdout);
	}
	sleep(1);//执行睡眠是因为如果不执行这行,主线程执行完以后程序就退出了
	//子线程执行到什么成度不一定
	printf("\n");
	return 0;
}

执行结果:

 

如果不使用sleep函数怎么实现线程同步问题?

可以使用pthread_join函数,第一个参数是子线程ID,第二个参数为空就可以了。

代码:

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define ERR_EXIT(m)				\
		do						\
		{						\
			perror(m);			\
			exit(EXIT_TAILURE);	\
		}while(0)
void* thread_routine(void* arg)
{//这个函数是创建线程所执行的函数,函数名称是
//创建线程函数的参数。这个函数的参数是也是由
//创建线程函数传递过来的
//线程执行是交替执行的
	for(int i = 0;i < 20; ++i)
	{
		printf("B");
		fflush(stdout);
	}
	return 0;
}
int main()
{
	pthread_t tid;
	int ret;

	if((ret = pthread_create(&tid,NULL,thread_routine,NULL)) != 0)
	{//创建线程的函数,成功返回0,失败返回错误码,所以if语句中
	 //可以直接打印错误码。
	//第一个参数是线程ID号,第二个参数是NULL,一般为空就可以了,
	//第三个参数是线程执行的函数,第四个参数是给线程传递的参数
		fprintf(stderr,"pthread_create:%s\n",strerror(ret));
		exit(EXIT_FAILURE);
	}

	for(int i = 0; i < 20;++i)
	{
		printf("A");
		fflush(stdout);
	}
//	sleep(1);//执行睡眠是因为如果不执行这行,主线程执行完以后程序就退出了
	//子线程执行到什么成度不一定
//不用sleep函数可以使用thread_join函数来实现等待
	if((ret = pthread_join(tid,NULL)) != 0)
	{
		fprintf(stderr,"pthread_join:%s\n",strerror(ret));
		exit(EXIT_FAILURE);
	}
	printf("\n");
	return 0;
}

有一个细节就是pthread_join函数第二个参数为空指针,其实这个参数是接收子线程返回的值得。

想要接收子线程返回的值,就要定义一个二维指针

比如:

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define ERR_EXIT(m)				\
		do						\
		{						\
			perror(m);			\
			exit(EXIT_TAILURE);	\
		}while(0)
void* thread_routine(void* arg)
{//这个函数是创建线程所执行的函数,函数名称是
//创建线程函数的参数。这个函数的参数是也是由
//创建线程函数传递过来的
//线程执行是交替执行的
	for(int i = 0;i < 20; ++i)
	{
		printf("B");
		fflush(stdout);
	}
	return "DEF";
}
int main()
{
	pthread_t tid;
	int ret;

	if((ret = pthread_create(&tid,NULL,thread_routine,NULL)) != 0)
	{//创建线程的函数,成功返回0,失败返回错误码,所以if语句中
	 //可以直接打印错误码。
	//第一个参数是线程ID号,第二个参数是NULL,一般为空就可以了,
	//第三个参数是线程执行的函数,第四个参数是给线程传递的参数
		fprintf(stderr,"pthread_create:%s\n",strerror(ret));
		exit(EXIT_FAILURE);
	}

	for(int i = 0; i < 20;++i)
	{
		printf("A");
		fflush(stdout);
	}
//	sleep(1);//执行睡眠是因为如果不执行这行,主线程执行完以后程序就退出了
	//子线程执行到什么成度不一定
//不用sleep函数可以使用thread_join函数来实现等待
	void* value;
	if((ret = pthread_join(tid,&value)) != 0)
	{
		fprintf(stderr,"pthread_join:%s\n",strerror(ret));
		exit(EXIT_FAILURE);
	}
	printf("\n");
	printf("return msg = %s\n",(char*)value);
	return 0;
}

执行结果:

 

pthread_exit函数的用法

执行到这条函数后可以停止线程的执行。参数是要返回的值,可以使用pthread_join 函数接收。

上代码:

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define ERR_EXIT(m)				\
		do						\
		{						\
			perror(m);			\
			exit(EXIT_TAILURE);	\
		}while(0)
void* thread_routine(void* arg)
{//这个函数是创建线程所执行的函数,函数名称是
//创建线程函数的参数。这个函数的参数是也是由
//创建线程函数传递过来的
//线程执行是交替执行的
	for(int i = 0;i < 20; ++i)
	{
		printf("B");
		fflush(stdout);
		if(i == 3)
		{
			pthread_exit("QWER");
		}
	}
//	return "DEF";
}
int main()
{
	pthread_t tid;
	int ret;

	if((ret = pthread_create(&tid,NULL,thread_routine,NULL)) != 0)
	{//创建线程的函数,成功返回0,失败返回错误码,所以if语句中
	 //可以直接打印错误码。
	//第一个参数是线程ID号,第二个参数是NULL,一般为空就可以了,
	//第三个参数是线程执行的函数,第四个参数是给线程传递的参数
		fprintf(stderr,"pthread_create:%s\n",strerror(ret));
		exit(EXIT_FAILURE);
	}

	for(int i = 0; i < 20;++i)
	{
		printf("A");
		fflush(stdout);
	}
//	sleep(1);//执行睡眠是因为如果不执行这行,主线程执行完以后程序就退出了
	//子线程执行到什么成度不一定
//不用sleep函数可以使用thread_join函数来实现等待
	void* value;
	if((ret = pthread_join(tid,&value)) != 0)
	{
		fprintf(stderr,"pthread_join:%s\n",strerror(ret));
		exit(EXIT_FAILURE);
	}
	printf("\n");
	printf("return msg = %s\n",(char*)value);
	return 0;
}

执行结果:

 

pthread_self函数

pthread_cancel函数

 pthread_detach函数

 

 进程和线程类比:

线程属性:

 并发级别

 线程特定数据

 

代码:

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define ERR_EXIT(m)				\
		do						\
		{						\
			perror(m);			\
			exit(EXIT_TAILURE);	\
		}while(0)

pthread_key_t key_tsd;

typedef struct tsd{
	pthread_t tid;
	char* str;
}tsd_t;

void destroy_routine(void* value)
{
	printf("destory....\n");
	free(value);
}
void* thread_routine(void* arg)
{
	tsd_t *value = (tsd_t*)malloc(sizeof(tsd_t));
	value -> tid = pthread_self();
	value -> str = (char*)arg;
	pthread_setspecific(key_tsd,value);
	printf("%s setpecific %p\n",(char*)arg,value);
	value = pthread_getspecific(key_tsd);
	printf("tid = 0x%x str = %s\n",(int)value -> tid,value -> str);
	sleep(2);
	value = pthread_getspecific(key_tsd);
	printf("tid = 0x%x str = %s\n",(int)value -> tid,value -> str);

	return NULL;
}
int main()
{
	pthread_key_create(&key_tsd,destroy_routine);
	pthread_t tid1;	
	pthread_t tid2;
	pthread_create(&tid1,NULL,thread_routine,"thread1");
	pthread_create(&tid2,NULL,thread_routine,"thread2");
	
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	
	pthread_key_delete(key_tsd);
	
	return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

波雅_汉库克

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值