• <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 閱讀(294) 評論(0)  編輯 收藏 引用 所屬分類: Linux
            精品一久久香蕉国产线看播放| 久久人人爽人人爽人人片av高请| 午夜天堂精品久久久久| 亚洲国产一成久久精品国产成人综合 | 国内精品久久久久久不卡影院| av无码久久久久久不卡网站 | 乱亲女H秽乱长久久久| 久久成人小视频| 亚洲熟妇无码另类久久久| 无码人妻久久一区二区三区| 亚洲中文久久精品无码ww16 | 久久综合久久性久99毛片| 青青久久精品国产免费看| 亚洲国产日韩欧美久久| 精品综合久久久久久98| 久久久久亚洲Av无码专| 久久777国产线看观看精品| 99久久99久久精品国产片果冻| 99久久精品免费看国产| 亚洲午夜精品久久久久久浪潮| 久久经典免费视频| 老色鬼久久亚洲AV综合| 久久久91精品国产一区二区三区| 久久这里只有精品首页| 亚洲精品tv久久久久| 97精品久久天干天天天按摩 | 日韩人妻无码一区二区三区久久99| 久久久久久国产精品无码下载 | 66精品综合久久久久久久| 久久人人爽人人爽AV片| 久久综合狠狠综合久久综合88| 久久线看观看精品香蕉国产| 午夜精品久久久久久影视777| 日日噜噜夜夜狠狠久久丁香五月| 国产精品一久久香蕉产线看| 久久久免费观成人影院| 久久精品国产亚洲AV无码娇色| 久久久久亚洲AV成人网人人软件| 97精品国产97久久久久久免费 | 免费精品国产日韩热久久| 国内精品久久久人妻中文字幕|