• <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
            18岁日韩内射颜射午夜久久成人 | 成人午夜精品无码区久久| 久久精品国产精品亚洲| 久久久WWW成人| 日本欧美久久久久免费播放网 | 最新久久免费视频| 99久久精品国产免看国产一区| 香蕉久久一区二区不卡无毒影院| 国产精品久久久久一区二区三区| 囯产极品美女高潮无套久久久 | 久久香蕉超碰97国产精品| 亚洲国产精品婷婷久久| 亚洲日本va午夜中文字幕久久| 久久无码人妻一区二区三区午夜| 久久久无码精品午夜| 99久久精品费精品国产一区二区 | 久久久久AV综合网成人| 久久久久久久久久久免费精品| 久久久久久亚洲AV无码专区| 亚洲国产精品嫩草影院久久| 成人国内精品久久久久影院VR| 精品乱码久久久久久久| 久久亚洲国产精品成人AV秋霞| 国产精品一区二区久久精品无码 | 超级碰碰碰碰97久久久久| 国产成人精品久久亚洲| 91久久婷婷国产综合精品青草| 99精品国产99久久久久久97| 亚洲精品国精品久久99热| 精品熟女少妇aⅴ免费久久| 国产亚洲精品美女久久久| 久久综合88熟人妻| 男女久久久国产一区二区三区| 久久午夜无码鲁丝片秋霞| 狠狠色丁香婷婷久久综合 | 国产精品永久久久久久久久久| 97久久综合精品久久久综合| 精品久久久久久成人AV| 波多野结衣中文字幕久久| 97精品国产91久久久久久| 国产欧美久久一区二区|