<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] # 簡介 信號量廣泛用于進程或線程間的同步和互斥,信號量本質上是一個非負的整數計數器,它被用來控制對公共資源的訪問。 編程時可根據操作信號量值的結果判斷是否對公共資源具有訪問的權限,當信號量值大于 0 時,則可以訪問,否則將阻塞。 PV 原語是對信號量的操作,一次 P 操作使信號量減1,一次 V 操作使信號量加1。 信號量主要用于進程或線程間的同步和互斥這兩種典型情況。 信號量數據類型為:sem\_t。 信號量用于互斥 ![](https://img.kancloud.cn/78/40/7840184c1741b897b05dac7b00602560_466x412.png) 信號量用于同步: ![](https://img.kancloud.cn/47/83/4783558814a374e3a6af0c2d97cab132_642x491.png) # sem_init函數 初始化信號量: ~~~ #include <semaphore.h> ? int sem_init(sem_t *sem, int pshared, unsigned int value); 功能: 創建一個信號量并初始化它的值。一個無名信號量在被使用前必須先初始化。 參數: sem:信號量的地址。 pshared:等于 0,信號量在線程間共享(常用);不等于0,信號量在進程間共享。 value:信號量的初始值。 返回值: 成功:0 失敗: - 1 ~~~ # sem_destroy函數 銷毀信號量: ~~~ #include <semaphore.h> ? int sem_destroy(sem_t *sem); 功能: 刪除 sem 標識的信號量。 參數: sem:信號量地址。 返回值: 成功:0 失敗: - 1 ~~~ ? # 信號量P操作(減1) ~~~ #include <semaphore.h> ? int sem_wait(sem_t *sem); 功能: 將信號量的值減 1。操作前,先檢查信號量(sem)的值是否為 0,若信號量為 0,此函數會阻塞,直到信號量大于 0 時才進行減 1 操作。 參數: sem:信號量的地址。 返回值: 成功:0 失敗: - 1 ? int sem_trywait(sem_t *sem); * 以非阻塞的方式來對信號量進行減 1 操作。 * 若操作前,信號量的值等于 0,則對信號量的操作失敗,函數立即返回。 ? int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout); * 限時嘗試將信號量的值減 1 * abs_timeout:絕對時間 ~~~ abs_timeout補充說明: ~~~ struct timespec { time_t tv_sec; /* seconds */ // 秒 long tv_nsec; /* nanosecondes*/ // 納秒 } ? time_t cur = time(NULL); //獲取當前時間。 struct timespec t; //定義timespec 結構體變量t t.tv_sec = cur + 1; // 定時1秒 sem_timedwait(&cond, &t); ~~~ # 信號量V操作(加1) ~~~ #include <semaphore.h> ? int sem_post(sem_t *sem); 功能: 將信號量的值加 1 并發出信號喚醒等待線程(sem_wait())。 參數: sem:信號量的地址。 返回值: 成功:0 失敗:-1 ~~~ # 獲取信號量的值 ~~~ #include <semaphore.h> ? int sem_getvalue(sem_t *sem, int *sval); 功能: 獲取 sem 標識的信號量的值,保存在 sval 中。 參數: sem:信號量地址。 sval:保存信號量值的地址。 返回值: 成功:0 失敗:-1 ~~~ # 例子 ~~~ sem_t sem; //信號量 void printer(char *str) { sem_wait(&sem);//減一 while (*str) { putchar(*str); fflush(stdout); str++; sleep(1); } printf("\n"); ? sem_post(&sem);//加一 } ? void *thread_fun1(void *arg) { char *str1 = "hello"; printer(str1); } ? void *thread_fun2(void *arg) { char *str2 = "world"; printer(str2); } ? int main(void) { pthread_t tid1, tid2; ? sem_init(&sem, 0, 1); //初始化信號量,初始值為 1 ? //創建 2 個線程 pthread_create(&tid1, NULL, thread_fun1, NULL); pthread_create(&tid2, NULL, thread_fun2, NULL); ? //等待線程結束,回收其資源 pthread_join(tid1, NULL); pthread_join(tid2, NULL); ? sem_destroy(&sem); //銷毀信號量 ? return 0; } ~~~ # 擴展 自旋鎖 > int pthread\_spin\_destroy(pthread\_spinlock\_t \*lock); > > int pthread\_spin\_init(pthread\_spinlock\_t \*lock, int pshared); 屏障 信號量: > int semget(key\_t key, int nsems, int semflg);
                  <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>

                              哎呀哎呀视频在线观看