• sem_wait sem_post信号量操作进本函数


    sem_wait   sem_post

    信号量的数据类型为结构sem_t,它本质上是一个长整型的数。函数sem_init()用来初始化一个信号量。它的原型为:  

    extern int sem_init __P ((sem_t *__sem, int __pshared, unsigned int __value));  

    sem为指向信号量结构的一个指针;pshared不为0时此信号量在进程间共享,否则只能为当前进程的所有线程共享;value给出了信号量的初始值。  

    函数sem_post( sem_t *sem )用来增加信号量的值。当有线程阻塞在这个信号量上时,调用这个函数会使其中的一个线程不在阻塞,选择机制同样是由线程的调度策略决定的。  

    函数sem_wait( sem_t *sem )被用来阻塞当前线程直到信号量sem的值大于0,解除阻塞后将sem的值减一,表明公共资源经使用后减少。函数sem_trywait ( sem_t *sem )是函数sem_wait()的非阻塞版本,它直接将信号量sem的值减一。  

    函数sem_destroy(sem_t *sem)用来释放信号量sem。 

    信号量用sem_init函数创建的,下面是它的说明:
      #include<semaphore.h>
     int sem_init (sem_t *sem, int pshared, unsigned int value);

     这个函数的作用是对由sem指定的信号量进行初始化,设置好它的共享选项,并指定一个整数类型的初始值。pshared参数控制着信号量的类型。如果 pshared的值是0,就表示它是当前里程的局部信号量;否则,其它进程就能够共享这个信号量。我们现在只对不让进程共享的信号量感兴趣。 (这个参数受版本影响), pshared传递一个非零将会使函数调用失败。

      这两个函数控制着信号量的值,它们的定义如下所示:

    [cpp] view plain copy
     
    1. #include <semaphore.h>  
    2.  int sem_wait(sem_t * sem);  
    3.  int sem_post(sem_t * sem);  
     这两个函数都要用一个由sem_init调用初始化的信号量对象的指针做参数。
     sem_post函数的作用是给信号量的值加上一个“1”,它是一个“原子操作”---即同时对同一个信号量做加“1”操作的两个线程是不会冲突的;而同时对同一个文件进行读、加和写操作的两个程序就有可能会引起冲突。信号量的值永远会正确地加一个“2”--因为有两个线程试图改变它。
     sem_wait函数也是一个原子操作,它的作用是从信号量的值减去一个“1”,但它永远会先等待该信号量为一个非零值才开始做减法。也就是说,如果你对一个值为2的信号量调用sem_wait(),线程将会继续执行,介信号量的值将减到1。如果对一个值为0的信号量调用sem_wait(),这个函数就会地等待直到有其它线程增加了这个值使它不再是0为止。如果有两个线程都在sem_wait()中等待同一个信号量变成非零值,那么当它被第三个线程增加一个“1”时,等待线程中只有一个能够对信号量做减法并继续执行,另一个还将处于等待状态。
     信号量这种“只用一个函数就能原子化地测试和设置”的能力下正是它的价值所在。还有另外一个信号量函数sem_trywait,它是sem_wait的非阻塞搭档。

     最后一个信号量函数是sem_destroy。这个函数的作用是在我们用完信号量对它进行清理。下面的定义:
     #include<semaphore.h>
     int sem_destroy (sem_t *sem);
     这个函数也使用一个信号量指针做参数,归还自己战胜的一切资源。在清理信号量的时候如果还有线程在等待它,用户就会收到一个错误。
     与其它的函数一样,这些函数在成功时都返回“0”。
     
    1. #include <stdio.h>  
    2. #include <unistd.h>  
    3. #include <stdlib.h>  
    4. #include <string.h>  
    5. #include <pthread.h>  
    6. #include <semaphore.h>  
    7. sem_t bin_sem;  
    8. void *thread_function1(void *arg)  
    9. {  
    10. printf("thread_function1--------------sem_wait ");  
    11. sem_wait(&bin_sem);  
    12. printf("sem_wait ");  
    13. while (1)  
    14. {  
    15. }  
    16. }  
    17. void *thread_function2(void *arg)  
    18. {  
    19. printf("thread_function2--------------sem_post ");  
    20. sem_post(&bin_sem);  
    21. printf("sem_post ");  
    22. while (1)  
    23. {  
    24. }  
    25. }  
    26. int main()  
    27. {  
    28. int res;  
    29. pthread_t a_thread;  
    30. void *thread_result;  
    31. res = sem_init(&bin_sem, 0, 0);  
    32. if (res != 0)  
    33. {  
    34.  perror("Semaphore initialization failed");  
    35. }  
    36.  printf("sem_init ");  
    37. res = pthread_create(&a_thread, NULL, thread_function1, NULL);  
    38. if (res != 0)  
    39. {  
    40.  perror("Thread creation failure");  
    41. }  
    42. printf("thread_function1 ");  
    43. sleep (5);  
    44. printf("sleep ");  
    45. res = pthread_create(&a_thread, NULL, thread_function2, NULL);  
    46. if (res != 0)  
    47. {  
    48.  perror("Thread creation failure");  
    49. }  
    50. while (1)  
    51. {  
    52. }  
    53. }  
    54. sem_init  
    55. thread_function1  
    56. thread_function1--------------sem_wait  
    57. sleep  
    58. thread_function2--------------sem_post  
    59. sem_wait  
    60. sem_post

    转自:sem_wait sem_post信号量操作进本函数

  • 相关阅读:
    加入收藏
    c#在窗口标题栏上加按钮转载自:http://tech.ddvip.com/200810/122483002782273.html
    关于获取c# 的winform中DataGird控件选中行的值
    GridView中使用LinkButton添加启用禁用功能
    winfrom定制窗体样式
    C#读取xml文件
    C#winform程序如何与js交互
    ASP.NET不允许输入空格
    Winform TextBox中只能输入数字的几种常用方法(C#)
    winfrom中的webbrowser与web里面的html以及js的交互
  • 原文地址:https://www.cnblogs.com/BinBinStory/p/7979173.html
Copyright © 2020-2023  润新知