• <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++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              32 隨筆 :: 0 文章 :: 25 評(píng)論 :: 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下線程與信號(hào)量例子
            posted on 2009-01-18 14:00 bujiwu 閱讀(294) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): Linux
            无码人妻少妇久久中文字幕| 久久99精品国产99久久6男男| 中文字幕亚洲综合久久菠萝蜜| 亚洲国产成人久久综合野外| 久久香综合精品久久伊人| 久久亚洲精品国产精品| 国产精品美女久久久久av爽 | 久久人人爽人人爽人人片av高请| 国产精品久久影院| 99久久国产亚洲综合精品| 国产精品久久久福利| 奇米影视7777久久精品人人爽| 久久久久亚洲av无码专区喷水| 999久久久免费国产精品播放| 国产精品美女久久福利网站| 久久午夜电影网| 国内精品久久久久久久97牛牛| 久久久免费观成人影院| 久久99国产综合精品免费| 思思久久99热免费精品6| 青青国产成人久久91网| 久久99国内精品自在现线| 99久久国产精品免费一区二区| 韩国三级中文字幕hd久久精品| 浪潮AV色综合久久天堂| 一本久久免费视频| 久久精品亚洲福利| 国内精品久久久久久久久| 国产一级做a爰片久久毛片| 久久久免费精品re6| 色欲av伊人久久大香线蕉影院| 久久久久久国产精品无码下载| 97久久国产亚洲精品超碰热| 狼狼综合久久久久综合网| 亚洲中文字幕无码久久2017| 欧美黑人激情性久久| 精品国产乱码久久久久软件| 亚洲一区精品伊人久久伊人| 久久久久久免费视频| 久久夜色精品国产亚洲| 国产亚洲美女精品久久久2020|