0
点赞
收藏
分享

微信扫一扫

linux线程-使用信号量同步

前程有光 2022-02-09 阅读 95

线程中访问临界区的方法

1、信号量:控制一组对象访问时,比如从5条可用的电话线中分配一个给某个线程的情况
2、互斥锁:想控制任一时刻只能有一个线程可以访问一些共享内存

本文使用二值信号量保护临界资源

include<semaphore.h>

//创建信号量
int sem_init(sem_t *sem,int pshared,unsigned int value);

//控制信号量的值
sem_wait(sem_t * sem);//信号量的值 -1
sem_post(sem_t * sem);//给信号量的值 +1

sem_destroy(sem_t *sem);//清理信号量所有资源

临界区资源 work_area

主线程获取输入,子线程统计字符串大小

#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<semaphore.h>
#include<pthread.h>

void *thread_function(void *arg);
sem_t bin_sem;


#define WORK_SIZE 1024

char work_area[WORK_SIZE];

int main()
{
	int res;
	pthread_t a_thread;

	void *thread_result;

	res = sem_init(&bin_sem,0,0);
	if(res != 0)
	{
		perror("Semaphore initialization failed\n");
		exit(EXIT_FAILURE);
	}

	res = pthread_create(&a_thread,NULL,thread_function,NULL);
	if(res !=0)
	{
		perror("Thread creation failed");
		exit(EXIT_FAILURE);
	}
	printf("Input some txt,Enter 'end' to finish\n");
	while(strncmp("end",work_area,3) !=0)
	{
		fgets(work_area,WORK_SIZE,stdin);
		sem_post(&bin_sem);
	}

	printf("\n Waiting for thread to finish...\n");
	res = pthread_join(a_thread,&thread_result);
	if(res !=0)
	{
		perror("Thread join failed");
		exit(EXIT_FAILURE);
	}
	printf("Thread joined\n");
	sem_destroy(&bin_sem);
	exit(EXIT_SUCCESS);



}

void *thread_function(void *arg)
{
	sem_wait(&bin_sem);
	while(strncmp("end",work_area,3) != 0)
	{
		printf("You input %d characters\n",strlen(work_area) -1);
		sem_wait(&bin_sem);
	}
	pthread_exit(NULL);
}

打印输出

(base) wy@ubuntu:~/process$ ./thread3 
Input some txt,Enter 'end' to finish
hello
You input 5 characters
end

 Waiting for thread to finish...
Thread joined
(base) wy@ubun
举报

相关推荐

0 条评论