本文基于信号量
如果信号量的值最多为1,那实际上相当于一个共享资源在任意时刻最多只能有一个线程在访问,这样的逻辑称为“互斥”。这时,有一种更加方便和语义更加准确的工具来满足这种逻辑---互斥锁。几个线程同时去抢一个锁,谁抢到了锁就进门把房间锁上,访问数据,出来后之前解锁。
照这样的逻辑,互斥锁的操作无非就是:
1.定义互斥锁。-----pthread_mutex_t类型
2.初始化锁。--------pthread_mutex_init()函数 int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
3.加锁。--------------pthread_mutex_lock()函数 int pthread_mutex_lock(pthread_mutex_t *mutex)
4.解锁。--------------pthread_mutex_unlock()函数 int pthread_mutex_unlock(pthread_mutex_t *mutex)
5.销毁互斥锁。----pthread_mutex_destroy()函数 int pthread_mutex_destroy(pthread_mutex_t *mutex)
C代码示例:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
/* 1 定义互斥锁变量 */
pthread_mutex_t m;
// 被访问的资源,全局变量
int a = 100;
void *routine(void *arg)
{
while (1)
{
/* 3 在访问资源之前,加锁 */
pthread_mutex_lock(&m);
// 访问资源
printf("副1线程获得锁,读到资源:a=%d,占用1秒\n", a);
sleep(1);
/* 4 访问完后,解锁 */
pthread_mutex_unlock(&m);
}
// 退出线程
pthread_exit(NULL);
}
void *routine1(void *arg)
{
while (1)
{
/* 3 在访问资源之前,加锁 */
pthread_mutex_lock(&m);
// 访问资源
printf("副2线程获得锁,读到资源:a=%d,占用2秒\n", a);
sleep(2);
/* 4 访问完后,解锁 */
pthread_mutex_unlock(&m);
}
// 退出线程
pthread_exit(NULL);
}
int main(int argc, char const *argv[])
{
/* 2 初始化互斥锁 */
pthread_mutex_init(&m, NULL);
// 创建线程
pthread_t tid, tid1;
pthread_create(&tid, NULL, routine, NULL);
pthread_create(&tid1, NULL, routine1, NULL);
sleep(1);
while (1)
{
/* 3 在访问资源之前,加锁 */
pthread_mutex_lock(&m);
// 访问资源
printf("主线程抢到锁,访问到到资源:a=%d, 占用3秒。\n", a);
sleep(3);
/* 4 访问完后,解锁 */
pthread_mutex_unlock(&m);
// 解锁完后休息一下,不然近水楼台先得月,会一直被主线程抢到,而且主线程会有优先权
sleep(1);
printf("\033[42m我休息一下,你们先抢!!\033[0m\n");
}
// 阻塞等待子线程退出、
pthread_join(tid, NULL);
pthread_join(tid1, NULL);
/* 5 销毁互斥锁 */
pthread_mutex_destroy(&m);
return 0;
}