互斥概念
所谓的互斥就是线程之间互相排斥,获得资源的线程排斥其它没有获得资源的线程。在多线程编程中,需要保证共享数据操作的安全性,引入了互斥锁的概念.每个对象对应一个互斥锁的标记.这个标记用来保证任意时刻只能有一个线程访问改对象。
从互斥锁的这种行为看,线程加锁和解锁之间的代码相当于一个独木桥,同意时刻只有一个线程能执行。从全局上看,在这个地方,所有并行运行的线程都变成了排队运行了。比较专业的叫法是同步执行,这段代码区域叫临界区。同步执行就破坏了线程并行性的初衷了,临界区越大破坏得越厉害。所以在实际应用中,应该尽量避免有临界区出现。实在不行,临界区也要尽量的小。如果连缩小临界区都做不到,那还使用多线程干嘛?
pthread_mutex_t互斥锁
Linux初始化和销毁互斥锁的接口是pthread_mutex_init()和pthead_mutex_destroy(),对于加锁和解锁则有pthread_mutex_lock()、pthread_mutex_trylock()和pthread_mutex_unlock()。这些接口的完整定义如下:
1:
pthread_mutex_init(pthread_mutex_t * mutex, const pthread_mutexattr_t *attr);
// 初始化锁变量mutex。
// attr为锁属性,NULL值为默认属性。
2:
pthread_mutex_lock(pthread_mutex_t *mutex);
// 加锁(阻塞操作)
3:
pthread_mutex_trylock(pthread_mutex_t *mutex);
// 试图加锁(不阻塞操作)
// 当互斥锁空闲时将占有该锁;否则立即返回
// 但是与2不一样的是当锁已经在使用的时候,返回为EBUSY,而不是挂起等待。
4:
pthread_mutex_unlock(pthread_mutex_t *mutex);
释放锁
5:pthread_mutex_destroy(pthread_mutex_t *mutex);
使用完后删除
pthread_mutex_t互斥锁案例
#include
#include
#include
#include
#include
#include
pthread_mutex_t g_mutex;
int g_lock_var = 0;
void* task1(void *args) {
int ret;
time_t end_time;
end_time = time(NULL) + 10;
printf("%s", "aaaaaaaa\n");
while (time(NULL) < end_time) {
ret = pthread_mutex_trylock(&g_mutex);
if ( EBUSY == ret) {
printf("thread1: the varible is locked by thread2.\n");
} else {
printf("thread1: lock the varialbe! \n");
++g_lock_var;
pthread_mutex_unlock(&g_mutex);
}
sleep(1);
}
return NULL;
}
void* task2(void *args) {
time_t end_time;
end_time = time(NULL) + 10;
while (time(NULL) < end_time) {
pthread_mutex_lock(&g_mutex);
printf("thread2: lock the variale!\n");
++g_lock_var;
sleep(1);
pthread_mutex_unlock( &g_mutex );
}
return NULL;
}
int main() {
pthread_t thread1, thread2;
pthread_mutex_init(&g_mutex, NULL);
pthread_create(&thread1, NULL, task1, NULL);
pthread_create(&thread2, NULL, task2, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_mutex_destroy(&g_mutex);
printf("g_lock_var= %d\n", g_lock_var);
return 0;
}
条件变量保障线程安全性
条件变量关键点在 "变量"上。根据 "条件"来选择是否在那里等待,等待允许通过的信号.
这个信号的控制是由另外一个线程来控制的。
初始化和销毁条件变量的接口是pthread_cond_init()和pthread_cond_destory();控制“事件”发生的接口是pthread_cond_signal()或pthread_cond_broadcast();等待“事件”发生的接口是pthead_cond_wait()或pthread_cond_timedwait()。
他们的完整定义如下:
int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
int pthread_cond_destory(pthread_cond_t *cond);
//
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
// 等待会无限期的等待
int pthread_cond_timedwait(pthread_cond_t *cond,pthread_mutex_t *mutex, const timespec *abstime);
// 限时的等待,等待一定时间如果"事件"依然没有发生则去做别的事情。
int pthread_cond_signal(pthread_cond_t *cond);
// 单播只有一个线程会得到 已经发生了的通知
int pthread_cond_broadcast(pthread_cond_t *cond);
// 广播所有线程都会得到事件通知的线程,所有也需要经过互斥锁控制的独木桥
条件变量 使用案例
//
// Created by 汪庭东 on 2020/4/12.
//
#include
#include
#include
#include
#define BUFFER_SIZE 5
pthread_mutex_t g_mutex;
pthread_cond_t g_cond;
typedef struct {
char buf[BUFFER_SIZE];
int count;
}buffer_t;
buffer_t g_share = {"", 0};
char g_ch = 'A';
void* producer(void *args) {
printf("producer start .....%d\n", g_share.count);
while (g_ch < 'Z') {
pthread_mutex_lock(&g_mutex);
printf("producer get lock...\n");
if (g_share.count < BUFFER_SIZE) {
g_share.buf[g_share.count++] = g_ch++;
printf("produer got char [%c]\n", g_ch - 1);
if (g_share.count == BUFFER_SIZE) {
printf("Producer singaling full. \n");
pthread_cond_signal(&g_cond);
}
}
sleep(1);
pthread_mutex_unlock(&g_mutex);
}
printf("producer exit.\n");
}
void* consumer(void *args) {
printf("consumer start ......\n");
while (g_ch < 'Z') {
pthread_mutex_lock(&g_mutex);
printf("consumer get lock .... \n");
printf("consumer cond wait .....\n");
pthread_cond_wait(&g_cond, &g_mutex);
printf("consumer condition wait get ....%d\n", g_share.count);
for (int i = 0; g_share.buf[i] && g_share.count; ++i) {
putchar(g_share.buf[i]);
--g_share.count;
sleep(1);
}
putchar("\n");
pthread_mutex_unlock(&g_mutex);
}
printf("consumer exit.");
return NULL;
}
int main() {
pthread_t producer_thread;
pthread_t consumer_thread;
pthread_mutex_init(&g_mutex, NULL);
pthread_cond_init(&g_cond, NULL);
pthread_create(&producer_thread, NULL, producer, NULL);
pthread_create(&consumer_thread, NULL, consumer, NULL);
pthread_join(producer_thread, NULL);
pthread_join(consumer_thread, NULL);
pthread_cond_destroy(&g_cond);
pthread_mutex_destroy(&g_mutex);
return 0;
}