【Linux篇】线程操作API

文章介绍了C++中使用pthread库进行线程创建、线程退出以及线程间通信的基本方法,包括pthread_create、pthread_exit和pthread_join,并提供了编程示例和线程共享内存空间的案例。
摘要由CSDN通过智能技术生成

一、线程创建

#include <pthread.h>
 
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);
参数:
pthread_t *thread:                 pthread是一个指针参数,传出线程ID
const pthread_attr_t *attr:        用于指定线程属性,传入NULL表示使用默认属性
void *(*start_routine) (void *):   是个函数指针,是线程的主控函数
void *arg:                         是第三个函数的参数,要强制转换成泛型(void*)然后进行值 
                                    传递即可,不能传递地址
 
返回值:若成功返回0,否则返回错误编号

二、线程退出

#include <pthread.h>

void pthread_exit(void *retval);
 
void *retval:    retval表示线程的退出值,我们必须将该参数强转为泛型void*

三、线程等待 

#include <pthread.h>
 
int pthread_join(pthread_t thread, void **retval);
 
参数:
pthread_t thread:  thread是我们要等待退出的线程的线程ID
void **retval:     retval是传出参数,用于获取线程的退出值,即,pthread_exit里的那个参数
 
 在使用该函数时要注意retval参数:
(1) 一定要用void**强制转换为泛型指针
(2)该函数是将pthread_exit里的退出值 复制到 retval所指向的位置。
(3)该参数可以置为NULL,表示不需要获取线程退出值。

四、编程示例

1.创建一个线程

#include <stdio.h>
#include <pthread.h>

void *func1(void *arg)
{
	printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
	printf("param is %d\n",*((int *)arg));
}

int main()
{
	int ret;
	int param = 100;
	pthread_t t1;

	ret = pthread_create(&t1,NULL,func1,(void *)&param);
	if(ret == 0){
		printf("main:create t1 success\n");
	}
	printf("main:%ld\n",(unsigned long)pthread_self());
	pthread_join(t1,NULL);

	return 0;
}

2.线程创建、等待、退出综合

#include <stdio.h>
#include <pthread.h>

void *func1(void *arg)
{
	static int ret = 10;

	printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
	printf("param is %d\n",*((int *)arg));

	pthread_exit((void *)&ret);
}

int main()
{
	int ret;
	int param = 100;
	pthread_t t1;

	int *pret = NULL;

	ret = pthread_create(&t1,NULL,func1,(void *)&param);
	if(ret == 0){
		printf("main:create t1 success\n");
	}
	printf("main:%ld\n",(unsigned long)pthread_self());
	pthread_join(t1,(void **)&pret);

	printf("main:t1 quit:%d\n",*pret);

	return 0;
}

 

3.线程共享内存空间的代码验证

#include <stdio.h>
#include <pthread.h>

int g_data = 0;

void *func1(void *arg)
{
	printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
	printf("t1:param is %d\n",*((int *)arg));
	while(1){
		printf("t1:%d\n",g_data++);
		sleep(1);
	}
}

void *func2(void *arg)
{
	printf("t2:%ld thread is create\n",(unsigned long)pthread_self());
	printf("t2:param is %d\n",*((int *)arg));
	while(1){
		printf("t2:%d\n",g_data++);
		sleep(1);
	}
}

int main()
{
	int ret;
	int param = 100;
	pthread_t t1;
	pthread_t t2;

	ret = pthread_create(&t1,NULL,func1,(void *)&param);
	if(ret == 0){
		printf("main:create t1 success\n");
	}
	ret = pthread_create(&t2,NULL,func2,(void *)&param);
	if(ret == 0){
		printf("main:create t2 success\n");
	}

	printf("main:%ld\n",(unsigned long)pthread_self());
	while(1){
		printf("main:%d\n",g_data++);
		sleep(1);	
	}

	pthread_join(t1,NULL);
	pthread_join(t2,NULL);

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序猿gao

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

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

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

打赏作者

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

抵扣说明:

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

余额充值