• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Jiwu Bu

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              32 隨筆 :: 0 文章 :: 25 評論 :: 0 Trackbacks
             1 #include <stdio.h>       /* standard I/O routines                 */
             2 #include <pthread.h>     /* pthread functions and data structures */
             3 #include <unistd.h>      /* head file for sleep function */
             4 
             5 /* global mutex for our program. assignment initializes it */
             6 pthread_mutex_t a_mutex = PTHREAD_MUTEX_INITIALIZER;
             7 
             8 /* function to be executed by the new thread */
             9 void* do_loop(void* data)
            10 {
            11     int me = *((int*)data);     /* thread identifying number */
            12     int rc; /* contain mutex lock/unlock results */
            13     
            14     /* lock the mutex, to assure exclusive */
            15     rc = pthread_mutex_lock(&a_mutex);
            16     
            17     for (int i=0; i< 4; i++
            18     {
            19         printf("'%d' - Got '%d'\n", me, i);
            20         sleep(1);
            21     }
            22 
            23     /* unlock mutex */
            24     rc = pthread_mutex_unlock(&a_mutex);
            25     
            26     /* terminate the thread */
            27     pthread_exit(NULL);
            28 }
            29 
            30 /* like any CPlusPlus program, program's execution begins in main */
            31 /*g++ -o Thread Thread.cpp -lpthread*/
            32 int main(int argc, char* argv[])
            33 {
            34     int        thr_id;         /* thread ID for the newly created thread */
            35     pthread_t  p_thread1;       /* thread's structure                     */
            36     pthread_t  p_thread2;       /* thread's structure                     */
            37     int        a = 1;  /* thread 1 identifying number            */
            38     int        b = 2;  /* thread 2 identifying number            */
            39 
            40     /* create a new thread that will execute 'do_loop()' */
            41     thr_id = pthread_create(&p_thread1, NULL, do_loop, (void*)&a);
            42     
            43     /* create a new thread */
            44     thr_id = pthread_create(&p_thread2, NULL, do_loop, (void*)&b);
            45     
            46     /*wait until sub thread exit*/
            47     pthread_join(p_thread1, NULL);
            48     pthread_join(p_thread2, NULL);
            49 
            50     return 0;
            51 }
            52 

            編譯: gcc -o pthread pthread.c  -lpthread


            Linux下線程與信號量例子
            posted on 2009-01-18 14:00 bujiwu 閱讀(300) 評論(0)  編輯 收藏 引用 所屬分類: Linux
            久久不见久久见免费影院www日本| 久久ww精品w免费人成| 精品国产婷婷久久久| 日本高清无卡码一区二区久久| 久久天天躁狠狠躁夜夜av浪潮| 伊人色综合久久天天网| 狠狠综合久久综合88亚洲| 91久久精品91久久性色| 久久久久18| 久久精品国产99久久无毒不卡 | 精品久久香蕉国产线看观看亚洲| 久久99精品久久久久子伦| 久久精品99无色码中文字幕| 精品多毛少妇人妻AV免费久久| 亚洲国产精品久久久久久| 久久妇女高潮几次MBA| 国产精品美女久久久免费| 久久久久久久97| 欧美一级久久久久久久大片| 2021久久国自产拍精品| 99久久国产宗和精品1上映| 久久精品国产99久久久香蕉| 97久久国产亚洲精品超碰热| 亚洲精品乱码久久久久久蜜桃不卡 | 夜夜亚洲天天久久| 久久久亚洲欧洲日产国码aⅴ| 久久精品亚洲精品国产欧美| 99精品久久精品| 久久久久亚洲AV无码麻豆| 久久人妻无码中文字幕| 亚洲国产高清精品线久久 | 人妻精品久久久久中文字幕一冢本| 久久99国产精品成人欧美| 99热精品久久只有精品| 精品999久久久久久中文字幕| 久久久久亚洲精品天堂| 性色欲网站人妻丰满中文久久不卡| 久久99久国产麻精品66| 精品综合久久久久久98| 欧美午夜精品久久久久免费视| 中文字幕无码免费久久|