linux-线程

本文详细介绍了线程的创建、回收、取消以及线程同步的相关函数,包括pthread_create、pthread_join、pthread_detach、pthread_cancel等。同时,讨论了信号量和互斥锁作为线程同步手段的应用,如sem_init、sem_wait、pthread_mutex_lock和pthread_mutex_unlock。此外,还提到了条件变量pthread_cond_init和pthread_cond_wait在多线程通信中的作用。

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

线程

线程常见函数

线程创建与回收

  1. pthread_create:主线程创建子线程
  2. pthread_join:主线程用来等待(阻塞)回收子线程
  3. pthread_detach:主线程用来分离子线程,分离后主线程不用回收子线程

线程取消

  1. pthread_cancel:一个线程(一般是主线程)可以通过调用该函数终止其他线程
  2. pthread_setcancelstate:一个线程是否可以被其他线程杀死,取决于该线程是否设置了权限,该函数就是设置自己是否允许被取消
  3. pthread_setcanceltype:这个函数必须线程被设置了取消才能起作用,作用是取消的类型(收到取消信号后立即取消和等待到该取消的时候退出)

线程函数退出相关函数

  1. pthread_exit与return退出
  2. pthread_cleanup_push:当子线程在还没解锁的情况下,就被主线程取消,这时会导致不能解锁。因此这个函数的作用是在子线程被取消后用来解锁的作用。
  3. pthread_cleanup_pop:这个函数的作用是用来接应pthread_cleanup_push。如果传的参数是0将pthread_cleanup_push里面的函数拿出来并且不执行它;1是将pthread_cleanup_push里面的函数拿出来并执行它。

线程id获取

  1. pthread_self

线程同步

信号量

  1. sem_init:初始化信号量
  2. sem_destroy:销毁信号量
  3. sem_post:信号量加1
  4. sem_wait:等待信号
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

char buf[200] = {0};
sem_t sem;
int flag = 0;

void *func(void *arg)
{
	while (1){
		sem_wait(&sem);
		if(1==flag){
			//exit(-1);
			printf("end....\n");
			pthread_exit(NULL);
		}
		printf("本次输入了%ld个字符\n",strlen(buf));
		memset(buf,0,sizeof(buf));
		
	}
}

int main()
{
	
	int ret = -1;
	pthread_t th = -1;
	sem_init(&sem,0,0);
	ret = pthread_create(&th,NULL,func,NULL);
	if(ret != 0){
		printf("pthread_creat error\n");
		exit(-1);
	}
	printf("请输入字符以回车结束\n");
	while (scanf("%s",buf)){
		if(strstr(buf,"end")){
			//exit(-1);
			flag = 1;
			sem_post(&sem);
			printf("end\n");
			break;
		}

		sem_post(&sem);
	}
	ret = pthread_join(th,NULL);
	if(ret != 0){
		printf("pthread_join error\n");
		exit(-1);
	}
	sem_destroy(&sem);
	return 0;
}

互斥锁

  1. pthread_mutex_init:互斥锁初始化
  2. pthread_mutex_destroy:互斥锁销毁
  3. pthread_mutex_lock:互斥锁上锁
  4. pthread_mutex_unlock:互斥锁解锁
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

char buf[200] = {0};
pthread_mutex_t mutex;
int flag = 0;
void *func(void *arg)
{
	while (1){
		if(1==flag){
			pthread_mutex_lock(&mutex);
			printf("本次输入了%ld个字符\n",strlen(buf));
			memset(buf,0,sizeof(buf));
			flag=0;
			if(strstr(buf,"end")){
				pthread_exit(NULL);
			}
			pthread_mutex_unlock(&mutex);
			
		}
		
		
		
	}
}

int main()
{
	
	int ret = -1;
	pthread_t th = -1;
	
	pthread_mutex_init(&mutex,NULL);
	
	ret = pthread_create(&th,NULL,func,NULL);
	if(ret != 0){
		printf("pthread_creat error\n");
		exit(-1);
	}
	printf("请输入字符以回车结束\n");
	while (1){
		if(0==flag){
			pthread_mutex_lock(&mutex);
			scanf("%s",buf);
			flag = 1;
			if(strstr(buf,"end")){
				//exit(-1);

				printf("end\n");
				exit(0);
			}
			pthread_mutex_unlock(&mutex);
		}
	}
	ret = pthread_join(th,NULL);
	if(ret != 0){
		printf("pthread_join error\n");
		exit(-1);
	}
	
	pthread_mutex_destroy(&mutex);
	return 0;
}

条件变量

  1. pthread_cond_init:条件变量初始化
  2. pthread_cond_destroy:销毁条件变量
  3. pthread_cond_wait:等待条件变量
  4. pthread_cond_signal(唤醒一个)/pthread_cond_broadcast(唤醒多个)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

char buf[200] = {0};
pthread_mutex_t mutex;
pthread_cond_t cond;
int flag = 0;
void *func(void *arg)
{
	while (1){
		if(1==flag){
			pthread_mutex_lock(&mutex);
			pthread_cond_wait(&cond,&mutex);
			printf("本次输入了%ld个字符\n",strlen(buf));
			memset(buf,0,sizeof(buf));
			flag=0;
			if(strstr(buf,"end")){
				pthread_exit(NULL);
			}
			pthread_mutex_unlock(&mutex);
			
		}
		
		
		
	}
}

int main()
{
	
	int ret = -1;
	pthread_t th = -1;
	
	pthread_mutex_init(&mutex,NULL);
	pthread_cond_init(&cond,NULL);
	ret = pthread_create(&th,NULL,func,NULL);
	if(ret != 0){
		printf("pthread_creat error\n");
		exit(-1);
	}
	printf("请输入字符以回车结束\n");
	while (1){
		if(0==flag){
			scanf("%s",buf);
			flag = 1;
			
			if(strstr(buf,"end")){
				//exit(-1);
				printf("end\n");
				exit(0);
			}

		}
		pthread_cond_signal(&cond);
	}
	ret = pthread_join(th,NULL);
	if(ret != 0){
		printf("pthread_join error\n");
		exit(-1);
	}
	
	pthread_mutex_destroy(&mutex);
	pthread_cond_destroy(&cond);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值