linux-线程同步

学习目标:

  • 熟练掌握互斥量的使用
  • 说出什么叫死锁以及解决方案
  • 熟练掌握读写锁的使用
  • 熟练掌握条件变量的使用
  • 理解条件变量实现的生产消费者模型
  • 理解信号量实现的生产消费者模型

1 互斥锁

1.1互斥锁的使用步骤

  • 第1步:创建一把互斥锁pthread_mutex_t mutex;
  • 第2步:初始化互斥锁pthread_mutex_init(&mutex);---相当于mutex=1
  • 第3步:在代码中寻找共享资源(也称为临界区)pthread_mutex_lock(&mutex); -- mutex = 0
    [临界区代码]pthread_mutex_unlock(&mutex); -- mutex = 1
  • 第4步:释放互斥锁资源pthread_mutex_destroy(&mutex);

注意:必须在所有操作共享资源的线程上都加上锁否则不能起到同步的效果。

1.2 练习

  1. 定义一把互斥锁,应该为一全局变量
    pthread_mutex_t mutex;
  2. 在main函数中对mutex进行初始化
    pthread_mutex_init(&mutex, NULL);
  3. 创建两个线程,在两个线程中加锁和解锁
  4. 主线程释放互斥锁资源
    pthread_mutex_destroy(&mutex);

在这里插入图片描述

1.3 死锁

死锁并不是linux提供给用户的一种使用方法,而是由于用户使用互斥锁不当引起的一种现象。
常见的死锁有两种:

  • 第一种:自己锁自己,如下图代码片段
    在这里插入图片描述

  • 第二种 线程A拥有A锁,请求获得B锁;线程B拥有B锁,请求获得A锁,这样造成线程A和线程B都不释放自己的锁,而且还想得到对方的锁,从而产生死锁,如下图所示:
    在这里插入图片描述
    如何解决死锁:

  • 让线程按照一定的顺序去访问共享资源

  • 在访问其他锁的时候,需要先将自己的锁解开

  • 调用pthread_mutex_trylock,如果加锁不成功会立刻返回

2 读写锁

读写锁也叫共享-独占锁。当读写锁以读模式锁住时,它是以共享模式锁住的;当它以写模式锁住时,它是以独占模式锁住的。写独占、读共享。

读写锁使用场合:读写锁非常适合于对数据结构读的次数远大于写的情况。

读写锁特性

  • 读写锁是“写模式加锁”时,解锁前,所有对该锁加锁的线程都会被阻塞。
  • 读写锁是“读模式加锁”时,如果线程以读模式对其加锁会成功;如果线程以写模式加锁会阻塞。
  • 读写锁是“读模式加锁”时, 既有试图以写模式加锁的线程,也有试图以读模式加锁的线程。那么读写锁会阻塞随后的读模式锁请求。优先满足写模式锁。读锁、写锁并行阻塞,写锁优先级高。

2.1 读写锁场景练习

  • 线程A加写锁成功, 线程B请求读锁
    • 线程B阻塞; 当线程A解锁之后, 线程B加锁成功
  • 线程A持有读锁, 线程B请求写锁
    • 线程B阻塞;当线程A解锁之后, 线程B加锁成功
  • 线程A拥有读锁, 线程B请求读锁
    • 线程B加锁成功
  • 线程A持有读锁, 然后线程B请求写锁, 然后线程C请求读锁
    • B阻塞,c阻塞 - 写的优先级高
    • A解锁,B线程加写锁成功,C继续阻塞
    • B解锁,C加读锁成功
  • 线程A持有写锁, 然后线程B请求读锁, 然后线程C请求写锁
    • BC阻塞
    • A解锁,C加写锁成功,B继续阻塞
    • C解锁,B加读锁成功

2.2 读写锁总结

读并行,写独占,当读写同时等待锁的时候写的优先级高。

2.3 读写锁主要操作函数

  1. 定义一把读写锁 pthread_rwlock_t rwlock;

  2. 初始化 int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr);

  3. 函数参数

    • rwlock-读写锁
    • attr-读写锁属性,传NULL为默认属性
  4. 销毁读写锁 int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);

  5. 加读锁 int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);

  6. 尝试加读锁 int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);

  7. 加写锁 int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);

  8. 尝试加写锁 int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);

  9. 解锁 int pthread_rwlock_unlock(&pthread_rwlock_t *rwlock);

2.4 练习:3个线程不定时写同一全局资源,5个线程不定时读同一全局资源。


/*************************************************************************************
 * @Descripttion : 读写锁测试例程:5个读线程、3个写线程不定时操作同一全局资源
 * @version      : 1.0
 * @Author       : liuziyan
 * @Date         : 2021-07-24 21:02:57
 * @LastEditors  : Lzy
 * @LastEditTime : 2021-07-24 22:06:06
 * @FilePath     : /day9/pthread_rwlock.c
 * @Copyright 2021 liuziyan, All Rights Reserved. 
 *************************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <stddef.h>

int number = 0;

//定义一把读写锁
pthread_rwlock_t rwlock;

//写线程回调函数
void *thread_write(void *arg)
{
    int i = *(int *)arg;
    int cur=0;

    while (1)
    {
        //加写锁
        pthread_rwlock_wrlock(&rwlock);

        cur = number;
        cur++;
        number = cur;
        printf("[%d]-W:[%d]\n",i,cur);

        //解锁
        pthread_rwlock_unlock(&rwlock);
        sleep(rand()%3);
    }
}

//读线程回调函数
void *thread_read(void *arg)
{
    int i = *(int *)arg;
    int cur = 0;
    while(1)
    {
        //加读锁
        pthread_rwlock_rdlock(&rwlock);

        cur = number;
        printf("[%d]-R:[%d]\n",i,cur);

        //解锁
        pthread_rwlock_unlock(&rwlock);
        sleep(rand()%3);
    }
}

int main()
{

    int arr[8];
    pthread_t thread[8];

    //读写锁初始化
    pthread_rwlock_init(&rwlock,NULL);

    //创建3个写线程
    for (size_t i = 0; i < 3; i++)
    {
        arr[i] = i;
        pthread_create(&thread[i], NULL, thread_write, &arr[i]);
    }

    //创建5个写线程
    for (size_t i = 3; i < 8; i++)
    {
        arr[i] = i;
        pthread_create(&thread[i], NULL, thread_read, &arr[i]);
    }

    for (size_t j = 0; j < 8; j++)
    {
        pthread_join(thread[j], NULL);
    }

    //销毁读写锁
    pthread_rwlock_destroy(&rwlock);

    return 0;

}

3 条件变量(链表生产者和消费者模型)

  • 条件本身不是锁!但它也可以造成线程阻塞。通常与互斥锁配合使用。给多线程提供一个会合的场所。
    • 使用互斥量保护共享数据;
    • 使用条件变量可以使线程阻塞, 等待某个条件的发生, 当条件满足的时候解除阻塞.
  • 条件变量的两个动作:
    • 条件不满足, 阻塞线程(自身线程解锁)
    • 条件满足, 通知阻塞的线程解除阻塞, 开始工作.(并自身线程加锁)

3.1 条件变量相关函数

  • 定义一个条件变量 pthread_cond_t cond;

3.1.1 pthread_cond_init()函数

  • int pthread_cond_init(pthread_cond_t *restrict cond,const pthread_condattr_t *restrict attr);
  • 函数参数:
    • cond: 条件变量
    • attr: 条件变量属性, 通常传NULL
  • 函数返回值:成功返回0, 失败返回错误号

3.1.2 pthread_cond_destroy()函数

  • int pthread_cond_destroy(pthread_cond_t *cond);
  • 函数描述: 销毁条件变量
  • 函数参数: 条件变量
  • 返回值: 成功返回0, 失败返回错误号

3.1.2 pthread_cond_wait()函数

  • int pthread_cond_wait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex);
  • 函数描述: 条件不满足, 引起线程阻塞并解锁;条件满足, 解除线程阻塞, 并加锁
  • 函数参数:
    • cond: 条件变量
    • mutex: 互斥锁变量
  • 函数返回值: 成功返回0, 失败返回错误号

3.1.3 pthread_cond_signal()函数

  • int pthread_cond_signal(pthread_cond_t *cond);
  • 函数描述: 唤醒至少一个阻塞在该条件变量上的线程
  • 函数参数: 条件变量
  • 函数返回值: 成功返回0, 失败返回错误号

3.2 使用条件变量的代码片段

在这里插入图片描述
上述代码中,生产者线程调用pthread_cond_signal函数会使消费者线程在pthread_cond_wait处解除阻塞。


/*************************************************************************************
 * @Descripttion : 条件变量pthread_cond_t进行生产者消费者模型。
 * @version      : 1.0
 * @Author       : liuziyan
 * @Date         : 2021-07-26 10:31:18
 * @LastEditors  : Lzy
 * @LastEditTime : 2021-07-26 11:27:46
 * @FilePath     : /day9/pthread_cond.c
 * @Copyright 2021 liuziyan, All Rights Reserved. 
 *************************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>

typedef struct node
{
	int data;
	struct node *next;
} NODE;

NODE *head = NULL;

//链表长度
int len = 0;

//定义一把锁
pthread_mutex_t mutex;

//定义条件变量
pthread_cond_t cond;

//生产者模型(头插法生成链表)
void *producer(void *args)
{
	NODE *pNode = NULL;
	while (1)
	{
		//生产一个节点
		pNode = (NODE *)malloc(sizeof(NODE));
		if (pNode == NULL)
		{
			perror("malloc error:");
			exit(-1);
		}
		//加锁
		pthread_mutex_lock(&mutex);

		len++;
		pNode->data = len;
		printf("P:[%d]\n", pNode->data);
		pNode->next = head;
		head = pNode;

		//解锁
		pthread_mutex_unlock(&mutex);

		//通知消费者线程解除阻塞
		pthread_cond_signal(&cond);

		//防止生产过快而死
		sleep(rand() % 3);
	}
	pthread_exit(NULL);
}

void *consumer(void *args)
{
	NODE *pNode = NULL;
	while (1)
	{
		//加锁
		pthread_mutex_lock(&mutex);
		
		if(head == NULL)
		{
			//若条件不满足,则阻塞等待并解锁
			//若条件满足(被生产者线程调用pthread_cond_signal函数通知),则加锁该线程解除阻塞。
			pthread_cond_wait(&cond,&mutex);
		}

		printf("C:[%d]\n", head->data);
		pNode = head;
		head = head->next;
		len--;

		//解锁
		pthread_mutex_unlock(&mutex);

		//释放该节点内存
		free(pNode);
		pNode = NULL;
		sleep(rand() % 3);
	}

	pthread_exit(NULL);
}

int main()
{
	int ret;
	pthread_t thread1;
	pthread_t thread2;

	//随机数种子
	srand(time(NULL));

	//互斥锁初始化
	pthread_mutex_init(&mutex, NULL);

	//条件变量初始化
	pthread_cond_init(&cond, NULL);

	ret = pthread_create(&thread1, NULL, producer, NULL);
	if (ret != 0)
	{
		printf("pthread_create error, [%s]\n", strerror(ret));
		return -1;
	}

	ret = pthread_create(&thread2, NULL, consumer, NULL);
	if (ret != 0)
	{
		printf("pthread_create error, [%s]\n", strerror(ret));
		return -1;
	}

	//等待线程结束
	pthread_join(thread1, NULL);
	pthread_join(thread2, NULL);

	//释放互斥锁
	pthread_mutex_destroy(&mutex);
	
	//释放条件变量
	pthread_cond_destroy(&cond);

	return 0;
}

3.3 多线程生产者与消费模型


/*************************************************************************************
 * @Descripttion : 	条件变量pthread_cond_signal激活多个线程同时激活。
 * 					注意访问地址错误问题!
 * @version      : 1.0
 * @Author       : liuziyan
 * @Date         : 2021-07-26 14:15:34
 * @LastEditors  : Lzy
 * @LastEditTime : 2021-07-26 14:34:48
 * @FilePath     : /day9/pthread_cond2.c
 * @Copyright 2021 liuziyan, All Rights Reserved. 
 *************************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>

typedef struct node
{
	int data;
	struct node *next;
} NODE;

NODE *head = NULL;

//链表长度
int len = 0;

//定义一把锁
pthread_mutex_t mutex;

//定义条件变量
pthread_cond_t cond;

//生产者模型(头插法生成链表)
void *producer(void *args)
{
	NODE *pNode = NULL;
	int n = *(int *)args;
	while (1)
	{
		//生产一个节点
		pNode = (NODE *)malloc(sizeof(NODE));
		if (pNode == NULL)
		{
			perror("malloc error:");
			exit(-1);
		}

		//加锁
		pthread_mutex_lock(&mutex);

		len++;
		pNode->data = len;
		printf("P[%d]:[%d]\n", n, pNode->data);
		pNode->next = head;
		head = pNode;

		//解锁
		pthread_mutex_unlock(&mutex);

		//通知至少一个消费者线程解除阻塞
		pthread_cond_signal(&cond);

		//防止生产过快而死
		sleep(rand() % 3);
	}
	pthread_exit(NULL);
}

void *consumer(void *args)
{
	
	NODE *pNode = NULL;
	int n = *(int *)args;
	while (1)
	{
		//加锁
		pthread_mutex_lock(&mutex);

		if (head == NULL)
		{
			//若条件不满足,则阻塞等待并解锁
			//若条件满足(被生产者线程调用pthread_cond_signal函数通知),则加锁该线程解除阻塞。
			pthread_cond_wait(&cond, &mutex);
		}
		if (head == NULL)
		{
			//这里是由于生产者端pthread_cond_signal激活多个消费端
			//但是若只有一个数据,第一个释放后,第二个再访问该head则会空则访问失误。
			//因此加一个判断,则去掉。 
			//这里的解锁是由于之前激活时已经取消阻塞,并加锁,因此不解锁,则会导致死锁。
			pthread_mutex_unlock(&mutex);
			continue;
		}
		printf("C[%d]:[%d]\n", n, head->data);
		pNode = head;
		head = head->next;
		len--;

		//解锁
		pthread_mutex_unlock(&mutex);

		//释放该节点内存
		free(pNode);
		pNode = NULL;
		sleep(rand() % 3);
	}

	pthread_exit(NULL);
}

int main()
{
	int ret;
	int arr[5];
	pthread_t thread1[5];
	pthread_t thread2[5];

	//随机数种子
	srand(time(NULL));

	//互斥锁初始化
	pthread_mutex_init(&mutex, NULL);

	//条件变量初始化
	pthread_cond_init(&cond, NULL);
	for (int i = 0; i < 5; i++)
	{
		arr[i] = i;
		ret = pthread_create(&thread1[i], NULL, producer, &arr[i]);
		if (ret != 0)
		{
			printf("pthread[%d]_create error, [%s]\n", i, strerror(ret));
			return -1;
		}
		ret = pthread_create(&thread2[i], NULL, consumer, &arr[i]);
		if (ret != 0)
		{
			printf("pthread[%d]_create error, [%s]\n", i, strerror(ret));
			return -1;
		}
	}
	for (size_t i = 0; i < 5; i++)
	{
		//等待线程结束
		pthread_join(thread1[i], NULL);
		pthread_join(thread2[i], NULL);
	}

	//释放互斥锁
	pthread_mutex_destroy(&mutex);

	//释放条件变量
	pthread_cond_destroy(&cond);

	return 0;
	
}

4 信号量

4.1 信号量介绍

信号量相当于多把锁, 可以理解为是加强版的互斥锁
在这里插入图片描述

4.2 相关函数

4.2.1 sem_init()函数

  • 定义信号量 sem_t sem;
  • int sem_init(sem_t *sem, int pshared, unsigned int value);
  • 函数描述: 初始化信号量
  • 函数参数:
    • sem: 信号量变量
    • pshared: 0表示线程同步, 1表示进程同步
    • value: 最多有几个线程操作共享数据
  • 函数返回值:成功返回0, 失败返回-1, 并设置errno值

4.2.1 sem_wait()函数

  • int sem_wait(sem_t *sem);
  • 函数描述: 调用该函数一次, 相当于sem–, 当sem为0的时候, 引起阻塞
  • 函数参数: 信号量变量
  • 函数返回值: 成功返回0, 失败返回-1, 并设置errno值

4.2.1 sem_post()函数

  • int sem_post(sem_t *sem);
  • 函数描述: 调用一次, 相当于sem++
  • 函数参数: 信号量变量
  • 函数返回值: 成功返回0, 失败返回-1, 并设置errno值

4.2.1 sem_trywait()函数

  • int sem_trywait(sem_t *sem);
  • 函数描述: 尝试加锁, 若失败直接返回, 不阻塞
  • 函数参数: 信号量变量
  • 函数返回值: 成功返回0, 失败返回-1, 并设置errno值

4.2.1 sem_destroy()函数

  • int sem_destroy(sem_t *sem);
  • 函数描述: 销毁信号量
  • 函数参数: 信号量变量
  • 函数返回值: 成功返回0, 失败返回-1, 并设置errno值

4.3 信号量代码片段:

在这里插入图片描述

 
/*************************************************************************************
 * @Descripttion : sem_t信号量模拟多线程生产者消费者模型。
 * @version      : 1.0
 * @Author       : liuziyan
 * @Date         : 2021-07-26 14:58:53
 * @LastEditors  : Lzy
 * @LastEditTime : 2021-07-26 15:07:40
 * @FilePath     : /day9/pthread_sem2.c
 * @Copyright 2021 liuziyan, All Rights Reserved. 
 *************************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>

typedef struct node
{
	int data;
	struct node *next;
} NODE;

NODE *head = NULL;

//链表长度
int len = 0;

//定义生产者和消费者信号
sem_t sem_producer;
sem_t sem_consumer;

//生产者模型(头插法生成链表)
void *producer(void *args)
{
	NODE *pNode = NULL;
	while (1)
	{
		//生产一个节点
		pNode = (NODE *)malloc(sizeof(NODE));

		if (pNode == NULL)
		{
			perror("malloc error:");
			exit(-1);
		}
		
		//加锁 相当于生产者--
		sem_wait(&sem_producer);

		len++;
		pNode->data = len;
		printf("P[%d]:[%d]\n", *(int *)args,pNode->data);
		pNode->next = head;
		head = pNode;

		//解锁 相当于消费者++
		sem_post(&sem_consumer);

		//防止生产过快而死
		sleep(rand() % 3);
	}
	pthread_exit(NULL);
}

void *consumer(void *args)
{
	NODE *pNode = NULL;
	while (1)
	{
		//加锁
		sem_wait(&sem_consumer);

		printf("C[%d]:[%d]\n", *(int *)args,head->data);
		pNode = head;
		head = head->next;
		len--;

		//解锁 生产者
		sem_post(&sem_producer);

		//释放该节点内存
		free(pNode);
		pNode = NULL;
		sleep(rand() % 3);

	}

	pthread_exit(NULL);
}

int main()
{
	int ret;
	int arr[5];
	pthread_t thread1[5];
	pthread_t thread2[4];

	//随机数种子
	srand(time(NULL));

	//int sem_init(sem_t *sem, int pshared, unsigned int value);
	sem_init(&sem_producer,0,5);
	sem_init(&sem_consumer,0,0);

	for (int i = 0; i < 5; i++)
	{
		arr[i] = i;
		ret = pthread_create(&thread1[i], NULL, producer, &arr[i]);
		if (ret != 0)
		{
			printf("pthread_create error, [%s]\n", strerror(ret));
			return -1;
		}
	}
	

	for (int i = 0; i < 4; i++)
	{
		arr[i] = i;
		ret = pthread_create(&thread2[i], NULL, consumer, &arr[i]);
		if (ret != 0)
		{
			printf("pthread_create error, [%s]\n", strerror(ret));
			return -1;
		}
	}

	//等待线程结束
	for (int i = 0; i < 5; i++)
	{
		pthread_join(thread1[i], NULL);
	}
	
	
	for (size_t i = 0; i < 4; i++)
	{
		pthread_join(thread2[i], NULL);
	}
	
	//释放信号量资源
	sem_destroy(&sem_producer);
	sem_destroy(&sem_consumer);

	return 0;

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Linux线程同步的方式有很多,以下是其中一些常见的: 1. 互斥锁(Mutex):用于保护共享资源,只允许一个线程访问共享资源。当一个线程获取到互斥锁后,其他线程就必须等待这个线程释放锁后才能获取锁。 2. 读写锁(Reader-Writer Lock):在读多写少的情况下,使用读写锁可以提高并发性能。读写锁允许多个线程同时读取共享资源,但只允许一个线程写入共享资源。 3. 条件变量(Condition Variable):用于等待某个条件满足时才执行。当一个线程等待某个条件变量时,它会被阻塞,直到另外一个线程发出信号,通知条件已经满足,该线程才会继续执行。 4. 信号量(Semaphore):用于控制同时访问共享资源的线程数量。信号量可以是计数信号量或二进制信号量。计数信号量可以用来控制多个线程同时访问一个资源的数量,而二进制信号量只允许一个线程访问资源。 5. 屏障(Barrier):用于让多个线程在某个点上同步执行。当多个线程执行到某个点时,它们会被阻塞,直到所有线程都到达该点,才会继续执行。 这些同步机制可以根据具体的场景选择使用。在实际开发中,需要考虑多线程的安全性、性能等因素,选择合适的同步机制来实现线程同步。 ### 回答2: 在Linux中,线程同步是指多个线程之间的操作需要协调,以确保它们在执行任务时能够按照预期的顺序进行。 线程同步的目的是确保多个线程共享的资源(如共享内存、文件、网络连接等)能够被有序地访问和操作,避免出现竞态条件和资源争夺等问题,确保程序的正确性和性能。 常见的线程同步机制包括互斥锁、条件变量、读写锁、信号量等。 互斥锁是最基本的一种线程同步机制,它可以确保在任何时候只有一个线程可以访问共享资源。当某个线程获取了互斥锁之后,其他线程必须等待该线程释放锁后才能继续执行。互斥锁通过使用标志位和原子操作来确保线程的互斥性。 条件变量是一种线程同步机制,它可以使线程在满足某些条件之前一直等待,从而避免忙等待和浪费资源。条件变量常与互斥锁一起使用,当共享资源不满足条件时,线程可以使用条件变量进入等待状态,直到该条件被满足,另一个线程发出信号来唤醒等待线程。 读写锁是一种用于多线程读写共享资源的机制,它允许多个线程同时进行读操作,但只允许一个线程进行写操作。读写锁可以提高程序的并发性能,但需要注意避免读-写之间的竞争条件。 信号量是一种基于计数器的线程同步机制,它可以控制共享资源的访问数量和顺序。信号量可以实现互斥锁、条件变量等多种功能,是一种比较通用的线程同步机制。 除了上述机制,Linux中还有其他一些线程同步工具和算法,如屏障、自旋锁、分段锁、标记等。不同的线程同步机制和算法适用于不同的场景和需求,需要根据具体情况进行选择和使用。 ### 回答3: 在Linux中,由于多线程同时访问共享资源可能导致竞争条件的出现,因此需要使用线程同步技术来避免这种情况。除了使用互斥锁和条件变量来实现线程同步之外,也可以使用Linux提供的信号量机制。 信号量是一个整数值,用于控制对共享资源的访问。它包括两个主要的操作:PV操作和初始化操作。PV操作分为两种:P操作(等待操作)和V操作(释放操作)。一个线程在访问共享资源之前,必须执行P操作,如果信号量的值为0,则该线程将被阻塞。当线程使用完共享资源后,必须执行V操作来释放信号量,并唤醒其他等待访问共享资源的线程。 在Linux中使用信号量需要包含头文件<sys/sem.h>,并使用semget函数创建一个新的信号量集。接着,使用semctl函数可对信号量进行初始化或者删除操作。使用semop函数可进行PV操作。 与互斥锁和条件变量相比,信号量机制的优点是可以在不同进程间进行线程同步,而且可以实现多个线程同时访问共享资源的问题。但是,使用信号量需要特别小心,因为它比互斥锁和条件变量更难调试,如果使用不当会导致死锁等问题。 总之,Linux提供了多种线程同步机制,开发人员需要根据实际需求选择合适的机制来避免竞争条件的问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值