• <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 閱讀(299) 評論(0)  編輯 收藏 引用 所屬分類: Linux
            国内精品伊人久久久久AV影院| 久久久久女教师免费一区| 国产精品亚洲综合久久| 精品伊人久久大线蕉色首页| 欧美噜噜久久久XXX| 国产综合久久久久| 日日狠狠久久偷偷色综合96蜜桃| 国产日韩久久久精品影院首页| 久久综合亚洲色HEZYO国产| 亚洲国产成人精品91久久久| 久久人人爽人人爽人人片av高请| 国产精品综合久久第一页 | 国产精品久久国产精品99盘| 久久激情亚洲精品无码?V| 久久久久久精品久久久久| 国产精品视频久久久| 久久夜色精品国产噜噜亚洲a| 久久综合久久久| 77777亚洲午夜久久多喷| 91精品观看91久久久久久| 亚洲国产精品无码久久SM | 人人妻久久人人澡人人爽人人精品| 久久精品夜夜夜夜夜久久| 欧美一区二区久久精品| 伊人久久大香线焦综合四虎| 伊人情人综合成人久久网小说| 国产精品九九久久免费视频| 久久婷婷国产综合精品| 久久久久久久波多野结衣高潮| 97精品国产97久久久久久免费| 69国产成人综合久久精品| 色偷偷久久一区二区三区| 色天使久久综合网天天| 欧美日韩中文字幕久久久不卡| 国产激情久久久久影院| 99久久国产亚洲高清观看2024 | 亚洲精品高清久久| 久久精品国产免费一区| 韩国三级中文字幕hd久久精品| 久久97久久97精品免视看秋霞| 国内精品人妻无码久久久影院|