python同步锁和互斥锁的区别_理解线程2 信号量和互斥量 理解线程同步

线程同步

了解线程信号量的基础知识,对深入理解python的线程会大有帮助。

当两个线程同时执行时,不可避免同时操作同一个变量或者文件等,所以需要有一组机制来确保他们能正确的运行:信号量和互斥量。信号量可以分为最简单的“二进制信号量”和更通用的“计数信号量”。信号量通常用来保护一段代码,使其每次只能被一个执行线程运行,这种情况下需要用到二进制信号量。有时候希望可以允许有限数目的线程执行一段指定代码,这就需要用到计数信号量。实际上,技术信号量是一种二进制信号量的逻辑扩展,实际两者调用的函数一样。

互斥量和信号量很相似,事实上他们可以互相通过对方来实现。但在实际应用中,对于一些情况使用其中一种更符合语义而且效果更好。

用信号量进行同步

#include

#include

#include

#include

#include

#include

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); /* 初始化信号量,并且设置初始值为0*/

if (res != 0) {

perror("Semaphore initialization failed");

exit(EXIT_FAILURE);

}

res = pthread_create(&a_thread, NULL, thread_function, NULL); /* 创建新线程 */

if (res != 0) {

perror("Thread creation failed");

exit(EXIT_FAILURE);

}

printf("Inout some text, Enter 'end' to finish\n");

while(strncmp("end", work_area, 3) != 0) { /* 当工作区内不是以end开头的字符串时...*/

fgets(work_area, WORK_SIZE, stdin); /* 从标准输入获取输入到worl_area */

sem_post(&bin_sem); /* 信号量+1 */

}

printf("\nWaiting 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); /* 等待信号量有大于0的值然后-1 */

while(strncmp("end", work_area, 3) != 0) {

printf("You input %ld characters\n", strlen(work_area)-1); /* 获取输入字符串长度 8*/

sem_wait(&bin_sem); /* 等待信号量有大于0的值然后-1 */

}

pthread_exit(NULL);

}

用互斥量进行同步

#include

#include

#include

#include

#include

#include

void *thread_function(void *arg);

pthread_mutex_t work_mutex;

#define WORK_SIZE 1024

char work_area[WORK_SIZE];

int time_to_exit = 0; /* 用来控制TODO*/

int main() {

int res;

pthread_t a_thread;

void *thread_result;

res = pthread_mutex_init(&work_mutex,NULL); /* 初始化一个互斥锁 */

if (res != 0) {

perror("Mutex initialization failed");

exit(EXIT_FAILURE);

}

res = pthread_create(&a_thread, NULL, thread_function, NULL); /* 创建一个新线程 */

if (res != 0) {

perror("Thread creation failed");

exit(EXIT_FAILURE);

}

pthread_mutex_lock(&work_mutex); /* 尝试对互斥量加锁 */

printf("Input some text, Enter 'end' to finish\n");

while(!time_to_exit) { /* 检查是不是该退出*/

fgets(work_area, WORK_SIZE, stdin); /* 从标准输入获取输入到work_area */

pthread_mutex_unlock(&work_mutex); /* 解锁互斥量 */

while(1) {

pthread_mutex_lock(&work_mutex);

if (work_area[0] != '\0') { /* 持续检查work_area 是否为空, 如果不为空继续等待,如果为空,则重新读取输入到work_area*/

pthread_mutex_unlock(&work_mutex);

sleep(1);

}

else {

break;

}

}

}

pthread_mutex_unlock(&work_mutex);

printf("\nWaiting 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");

pthread_mutex_destroy(&work_mutex);

exit(EXIT_SUCCESS);

}

void *thread_function(void *arg) {

sleep(1);

pthread_mutex_lock(&work_mutex); /* 尝试加锁互斥量 */

while(strncmp("end", work_area, 3) != 0) { /* 当work_area里的值不是以end开头时*/

printf("You input %ld characters\n", strlen(work_area) -1); /* 输出输入的字符长度 */

work_area[0] = '\0'; /* work_area设置为空 */

pthread_mutex_unlock(&work_mutex);

sleep(1);

pthread_mutex_lock(&work_mutex);

while (work_area[0] == '\0') { /* 持续检查work_area 直到它里面有输入值*/

pthread_mutex_unlock(&work_mutex);

sleep(1);

pthread_mutex_lock(&work_mutex);

}

}

time_to_exit = 1; /* 当输入end后,设置退出标志 */

work_area[0] = '\0';

pthread_mutex_unlock(&work_mutex);

pthread_exit(0);

}

参考资料

《Beginning Linux Programming》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值