<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 功能強大 支持多語言、二開方便! 廣告
                [TOC] ## 自旋鎖 - 自旋鎖也是一種多線程同步的變量 - 使用自旋鎖的線程會反復檢査鎖變量是否可用 - 自旋鎖不會讓出CPU,是一種忙等待狀態 - **死循環等待鎖被釋放** **優點** - 自旋鎖避免了進程或線程上下文切換的開銷 - 操作系統內部很多地方使用的是自旋鎖 - 自旋鎖**不適合在單核CPU**使用 API 是 `pthread_spinlock_t` ## 示例 <details> <summary>main.cpp</summary> ``` #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #include <vector> pthread_spinlock_t spin_lock; int num =0; void *producer(void*){ int times = 10000000; while(times--){ pthread_spin_lock(&spin_lock); num++; pthread_spin_unlock(&spin_lock); } } void *comsumer(void*){ int times = 10000000; while(times--){ pthread_spin_lock(&spin_lock); num--; pthread_spin_unlock(&spin_lock); } } int main(){ printf("stat in main function."); pthread_spin_init(&spin_lock,0); pthread_t thread1,thread2; pthread_create(&thread1,NULL,&producer,NULL); pthread_create(&thread2,NULL,&comsumer,NULL); pthread_join(thread1,NULL); pthread_join(thread2,NULL); printf("print in main function:num=%d",num); return 0; } ``` </details> <br /> 編譯 ``` g++ main.cpp -lpthread ./a.out ```
                  <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>

                              哎呀哎呀视频在线观看