<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 同步,第 2 部分:計算信號量 > 原文:<https://github.com/angrave/SystemProgramming/wiki/Synchronization%2C-Part-2%3A-Counting-Semaphores> ## 什么是計數信號量? 計數信號量包含一個值,并支持兩個操作“等待”和“發布”。 Post 遞增信號量并立即返回。如果計數為零,“等待”將等待。如果計數不為零,則信號量遞減計數并立即返回。 類比是餅干罐中的餅干(或寶箱中的金幣)的計數。在拿餅干之前,請撥打“等待”。如果沒有剩下的 cookie,那么`wait`將不會返回:它將`wait`直到另一個線程通過調用 post 增加信號量。 簡而言之,`post`遞增并立即返回,而如果計數為零,`wait`將等待。在返回之前它將減少計數。 ## 如何創建信號量? 本頁介紹了未命名的信號量。不幸的是 Mac OS X 還不支持這些。 首先確定初始值是零還是其他值(例如,數組中剩余空格的數量)。與 pthread 互斥鎖不同,沒有創建信號量的快捷方式 - 使用`sem_init` ```c #include <semaphore.h> sem_t s; int main() { sem_init(&s, 0, 10); // returns -1 (=FAILED) on OS X sem_wait(&s); // Could do this 10 times without blocking sem_post(&s); // Announce that we've finished (and one more resource item is available; increment count) sem_destroy(&s); // release resources of the semaphore } ``` ## 我可以從不同的線程調用等待和發布嗎? 是!與互斥鎖不同,增量和減量可以來自不同的線程。 ## 我可以使用信號量而不是互斥量嗎? 是的 - 雖然信號量的開銷更大。要使用信號量: * 用一個計數器初始化信號量。 * 用`sem_wait`替換`...lock` * 用`sem_post`替換`...unlock` 互斥量是一個信號量,它始終是`waits` `posts` ```c sem_t s; sem_init(&s, 0, 1); sem_wait(&s); // Critical Section sem_post(&s); ``` ## 我可以在信號處理程序中使用 sem_post 嗎? 是! `sem_post`是可以在信號處理程序中正確使用的少數幾個函數之一。這意味著我們可以釋放一個等待線程,該線程現在可以使我們不允許在信號處理程序本身內調用的所有調用(例如`printf`)。 ```c #include <stdio.h> #include <pthread.h> #include <signal.h> #include <semaphore.h> #include <unistd.h> sem_t s; void handler(int signal) { sem_post(&s); /* Release the Kraken! */ } void *singsong(void *param) { sem_wait(&s); printf("I had to wait until your signal released me!\n"); } int main() { int ok = sem_init(&s, 0, 0 /* Initial value of zero*/); if (ok == -1) { perror("Could not create unnamed semaphore"); return 1; } signal(SIGINT, handler); // Too simple! See note below pthread_t tid; pthread_create(&tid, NULL, singsong, NULL); pthread_exit(NULL); /* Process will exit when there are no more threads */ } ``` 注意,健壯的程序不會在多線程程序中使用`signal()`(“多線程進程中的信號()的效果未指定。” - 信號手冊頁);一個更正確的程序需要使用`sigaction`。 ## 我怎么知道更多? 閱讀手冊頁: * [sem_init](http://man7.org/linux/man-pages/man3/sem_init.3.html) * [sem_wait](http://man7.org/linux/man-pages/man3/sem_wait.3.html) * [sem_post](http://man7.org/linux/man-pages/man3/sem_post.3.html) * [sem_destroy](http://man7.org/linux/man-pages/man3/sem_destroy.3.html)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看