信号量实现同步
来源:互联网 发布:windows snmp 工具 编辑:程序博客网 时间:2024/05/17 07:15
http://blog.csdn.net/wtz1985/article/details/3835781
在上一篇文章中已经用信号量来实现线程间的互斥,达到了互斥锁的效果,今天这篇文章将讲述怎样用信号量去实现同步。
信号量的互斥同步都是通过PV原语来操作的,我们可以通过注册两个信号量,让它们在互斥的问题上互动,从而达到同步。通过下面实例就可以很容易理解:
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <semaphore.h>
- #include <errno.h>
- #define return_if_fail(p) if((p) == 0){printf ("[%s]:func error!/n", __func__);return;}
- typedef struct _PrivInfo
- {
- sem_t s1;
- sem_t s2;
- time_t end_time;
- }PrivInfo;
- static void info_init (PrivInfo* thiz);
- static void info_destroy (PrivInfo* thiz);
- static void* pthread_func_1 (PrivInfo* thiz);
- static void* pthread_func_2 (PrivInfo* thiz);
- int main (int argc, char** argv)
- {
- pthread_t pt_1 = 0;
- pthread_t pt_2 = 0;
- int ret = 0;
- PrivInfo* thiz = NULL;
- thiz = (PrivInfo* )malloc (sizeof (PrivInfo));
- if (thiz == NULL)
- {
- printf ("[%s]: Failed to malloc priv./n");
- return -1;
- }
- info_init (thiz);
- ret = pthread_create (&pt_1, NULL, (void*)pthread_func_1, thiz);
- if (ret != 0)
- {
- perror ("pthread_1_create:");
- }
- ret = pthread_create (&pt_2, NULL, (void*)pthread_func_2, thiz);
- if (ret != 0)
- {
- perror ("pthread_2_create:");
- }
- pthread_join (pt_1, NULL);
- pthread_join (pt_2, NULL);
- info_destroy (thiz);
- return 0;
- }
- static void info_init (PrivInfo* thiz)
- {
- return_if_fail (thiz != NULL);
- thiz->end_time = time(NULL) + 10;
- sem_init (&thiz->s1, 0, 1);
- sem_init (&thiz->s2, 0, 0);
- return;
- }
- static void info_destroy (PrivInfo* thiz)
- {
- return_if_fail (thiz != NULL);
- sem_destroy (&thiz->s1);
- sem_destroy (&thiz->s2);
- free (thiz);
- thiz = NULL;
- return;
- }
- static void* pthread_func_1 (PrivInfo* thiz)
- {
- return_if_fail (thiz != NULL);
- while (time(NULL) < thiz->end_time)
- {
- sem_wait (&thiz->s2);
- printf ("pthread1: pthread1 get the lock./n");
- sem_post (&thiz->s1);
- printf ("pthread1: pthread1 unlock/n");
- sleep (1);
- }
- return;
- }
- static void* pthread_func_2 (PrivInfo* thiz)
- {
- return_if_fail (thiz != NULL);
- while (time (NULL) < thiz->end_time)
- {
- sem_wait (&thiz->s1);
- printf ("pthread2: pthread2 get the unlock./n");
- sem_post (&thiz->s2);
- printf ("pthread2: pthread2 unlock./n");
- sleep (1);
- }
- return;
- }
通过执行结果后,可以看出,会先执行线程二的函数,然后再执行线程一的函数。它们两就实现了同步。在上大学的时候,虽然对这些概念知道,可都没有实践过,所以有时候时间一久就会模糊甚至忘记,到了工作如果还保持这么一种状态,那就太可怕了。虽然现在外面的技术在不断的变化更新,可是不管怎么变,其核心技术还是依旧的,所以我们必须要打好自己的基础,再学习其他新的知识,那时候再学新的知识也会觉得比较简单的。闲话多说了两句,在下一篇文章中,我们将会实现一个经典的实例回顾这段时间对多线程的学习,那就是消费者和生产者。
- 信号量实现同步
- 信号量实现进程同步
- 信号量实现线程同步
- 使用信号量实现同步
- windows信号量实现读写同步
- 利用信号量实现线程同步
- 【Linux】 多线程编程(信号量实现同步)
- 信号量实现内核线程同步例子
- 使用信号量实现线程间同步
- 多线程之实现同步的信号量Semaphore
- 同步对象和信号量的实现
- C++使用信号量对象实现线程同步
- 线程同步之semaphore信号量,代码实现
- linux信号量实现线程读写同步
- 操作系统(Linux)多线程--信号量实现同步
- 信号量实现多进程的同步访问
- 利用信号量实现NSURLSession的同步请求
- 无名信号量实现相关进程同步
- HSIC Description
- Codeforces Round #140 (Div. 1) B Naughty Stone Piles
- 什么是sp,怎么运作,他们是怎么发财的
- 语境驱动测试7原则
- 关于黑盒跟白盒
- 信号量实现同步
- 命令行使用nmake设置化境变量
- escape() VS encodeURI() VS encodeURIComponent()
- Clonezilla
- ruby 数组 习惯用法
- NET Action Func Lambda
- JUnit4 详解
- loading过程中的蒙版问题
- powerpc交叉编译工具链的制作