Linux的线程学习

Linux线程与进程:
link.https://www.cnblogs.com/xiehongfeng100/p/4620852.html

实例
线程创建与退出

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

//int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
// 返回:若成功返回0,否则返回错误编号
//int pthread_exit(void *rval_ptr);
//int pthread_join(pthread_t thread, void **rval_ptr);
// 返回:若成功返回0,否则返回错误编号
void *func1(void *arg)
{
	static int ret = 10;
	static char *data = "wo shi shuai ge ";
	printf("t1 :%ld thread is cread\n",(unsigned long)pthread_self());
	printf("arg : %d\n",*(int *)arg);
	
	pthread_exit((void *)data);
}

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

	char *pret = NULL;
	ret = pthread_create(&t1,NULL,func1,(void *)&arg);
	if(ret == 0){
		printf("creat t1 ok\n");
	}

	printf("main :%ld \n",(unsigned long)pthread_self());
	
	pthread_join(t1,(void **)&pret);
	printf("main : ti quit :%s\n",pret);
	return 0;
}

线程共享内存空间

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

//int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
// 返回:若成功返回0,否则返回错误编号
//int pthread_exit(void *rval_ptr);
//int pthread_join(pthread_t thread, void **rval_ptr);
// 返回:若成功返回0,否则返回错误编号

int data = 0;
void *func1(void *arg)
{
	printf("t1 :%ld thread is cread\n",(unsigned long)pthread_self());
	printf("t1 :arg = %d\n",*(int *)arg);
	while(1){
		printf("t1: %d\n",data++);
		sleep(1);
	}
}
void *func2(void *arg)
{
	printf("t2 :%ld thread is cread\n",(unsigned long)pthread_self());
	printf("t2 :arg = %d\n",*(int *)arg);
	while(1){
		printf("t2: %d\n",data++);
		sleep(2);
	}
}

int main()
{
	int ret1,ret2;
	int arg = 100;
	pthread_t t1;
	pthread_t t2;

	ret1 = pthread_create(&t1,NULL,func1,(void *)&arg);
	if(ret1 == 0){
		printf("creat t1 ok\n");
	}
	ret2 = pthread_create(&t2,NULL,func2,(void *)&arg);
	if(ret2 == 0){
		printf("creat t2 ok\n");
	}
	
	printf("main: %ld\n",(unsigned long)pthread_self());
	while(1){
		printf("main: %d\n",data++);
		sleep(1);
	}
	pthread_join(t1,NULL);
	pthread_join(t2,NULL);
	return 0;

}

互斥锁加锁解锁

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

//int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
//int pthread_mutex_destroy(pthread_mutex_t mutex);
// 返回:若成功返回0,否则返回错误编号
//int pthread_mutex_lock(pthread_mutex_t mutex);
//int pthread_mutex_trylock(pthread_mutex_t mutex);
//int pthread_mutex_unlock(pthread_mutex_t mutex);
pthread_mutex_t mutex;
void *func1(void *arg)
{
	pthread_mutex_lock(&mutex);
	printf("t1 :%ld thread is cread\n",(unsigned long)pthread_self());
	printf("t1 :arg = %d\n",*(int *)arg);
	pthread_mutex_unlock(&mutex);
}
void *func2(void *arg)
{
	int i ;
	pthread_mutex_lock(&mutex);
	for(i = 0;i < 5;i++){
		printf("t2 :%ld thread is cread\n",(unsigned long)pthread_self());
		printf("t2 :arg = %d\n",*(int *)arg);
	}
	pthread_mutex_unlock(&mutex);
}

void *func3(void *arg)
{
	pthread_mutex_lock(&mutex);
	printf("t3 :%ld thread is cread\n",(unsigned long)pthread_self());
	printf("t3 :arg = %d\n",*(int *)arg);
	pthread_mutex_unlock(&mutex);
}

int main()
{
	int ret1,ret2,ret3;
	int arg = 100;
	pthread_t t1;
	pthread_t t2;
	pthread_t t3;
	pthread_mutex_init(&mutex ,NULL);
	ret1 = pthread_create(&t1,NULL,func1,(void *)&arg);
	if(ret1 == 0){
		printf("creat t1 ok\n");
	}
	ret2 = pthread_create(&t2,NULL,func2,(void *)&arg);
	if(ret2 == 0){
		printf("creat t2 ok\n");
	}
	ret3 = pthread_create(&t3,NULL,func3,(void *)&arg);
	if(ret3 == 0){
		printf("creat t3 ok\n");
	}
	printf("main: %ld\n",(unsigned long)pthread_self());
	pthread_join(t1,NULL);
	pthread_join(t2,NULL);
	pthread_join(t3,NULL);
	pthread_mutex_destroy(&mutex);
	return 0;

}

死锁

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

//int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
//int pthread_mutex_destroy(pthread_mutex_t mutex);
// 返回:若成功返回0,否则返回错误编号
//int pthread_mutex_lock(pthread_mutex_t mutex);
//int pthread_mutex_trylock(pthread_mutex_t mutex);
//int pthread_mutex_unlock(pthread_mutex_t mutex);
pthread_mutex_t mutex;
pthread_mutex_t mutex2;
void *func1(void *arg)
{
	pthread_mutex_lock(&mutex);
	sleep(1);
	pthread_mutex_lock(&mutex2);

	printf("t1 :%ld thread is cread\n",(unsigned long)pthread_self());
	printf("t1 :arg = %d\n",*(int *)arg);
	pthread_mutex_unlock(&mutex);
}
void *func2(void *arg)
{
	int i ;
	pthread_mutex_lock(&mutex2);
	sleep(1);
	pthread_mutex_lock(&mutex);
	for(i = 0;i < 5;i++){
		printf("t2 :%ld thread is cread\n",(unsigned long)pthread_self());
		printf("t2 :arg = %d\n",*(int *)arg);
	}
	pthread_mutex_unlock(&mutex);
}

void *func3(void *arg)
{
	pthread_mutex_lock(&mutex);
	printf("t3 :%ld thread is cread\n",(unsigned long)pthread_self());
	printf("t3 :arg = %d\n",*(int *)arg);
	pthread_mutex_unlock(&mutex);
}

int main()
{
	int ret1,ret2,ret3;
	int arg = 100;
	pthread_t t1;
	pthread_t t2;
	pthread_t t3;
	pthread_mutex_init(&mutex ,NULL);
	pthread_mutex_init(&mutex2 ,NULL);
	ret1 = pthread_create(&t1,NULL,func1,(void *)&arg);
	if(ret1 == 0){
		printf("main:creat t1 ok\n");
	}
	ret2 = pthread_create(&t2,NULL,func2,(void *)&arg);
	if(ret2 == 0){
		printf("main:creat t2 ok\n");
	}
	ret3 = pthread_create(&t3,NULL,func3,(void *)&arg);
	if(ret3 == 0){
		printf("main:creat t3 ok\n");
	}
	printf("main: %ld\n",(unsigned long)pthread_self());
	pthread_join(t1,NULL);
	pthread_join(t2,NULL);
	pthread_join(t3,NULL);
	pthread_mutex_destroy(&mutex);
	return 0;

}

条件控制线程

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<stdlib.h>
//int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
//int pthread_cond_destroy(pthread_cond_t cond);
//int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
//int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, cond struct timespec *restrict timeout);
//int pthread_cond_signal(pthread_cond_t cond);
//int pthread_cond_broadcast(pthread_cond_t cond);


pthread_mutex_t mutex =	PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int data = 0;
static int ret = 0;

void *func1(void *arg)
{
	while(1){
		pthread_cond_wait(&cond,&mutex);
		printf("t1 :%ld thread is cread\n",(unsigned long)pthread_self());
		printf("t1 :%d \n",data);
		data = 0;
		sleep(1);
		if(ret++ == 5){
			exit(1);	
		}
	}
}

void *func2(void *arg)
{
	printf("t2 :%ld thread is cread\n",(unsigned long)pthread_self());
	printf("t2 :arg = %d\n",*(int *)arg);
	while(1){
		printf("t2: %d\n",data);
		pthread_mutex_lock(&mutex);
		data++;
		pthread_mutex_unlock(&mutex);
		if(data == 5){
			pthread_cond_signal(&cond);
		}
		sleep(1);
	}
}

int main()
{
	int ret1,ret2;
	int arg = 100;
	pthread_t t1;
	pthread_t t2;
//	pthread_mutex_init(&mutex,NULL);
//	pthread_cond_init(&cond,NULL);
	ret1 = pthread_create(&t1,NULL,func1,(void *)&arg);
	if(ret1 == 0){
		printf("creat t1 ok\n");
	}
	ret2 = pthread_create(&t2,NULL,func2,(void *)&arg);
	if(ret2 == 0){
		printf("creat t2 ok\n");
	}
	pthread_join(t1,NULL);
	pthread_join(t2,NULL);
	pthread_mutex_destroy(&mutex);
	pthread_cond_destroy(&cond);
	return 0;

}

phtread条件变量pthread_cond_t初始化方式:动态初始化和静态初始化
link.https://blog.csdn.net/u010164190/article/details/106841719/

2023年12月 4日了解到了一个线程分离函数int pthread_detach(pthread_t thread)
例如以下场景:
线程分离状态:指定该状态,线程主动与主控线程断开关系。使用pthread_exit或者线程自动结束后,其退出状态不由其他线程获取,而直接自己自动释放。网络、多线程服务器常用。
进程若有该机制,将不会产生僵尸进程。僵尸进程的产生主要由于进程死后,大部分资源被释放,一点残留资源仍存于系统中,导致内核认为该进程仍存在。
此时可以使用pthread_detach函数实现线程分离,也可以使用 pthread_create函数参2(线程属性)来设置线程分离。pthread_detach函数是在创建线程之后调用的。

link.https://blog.csdn.net/m0_60663280/article/details/121589479

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值