• <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
            久久久久久无码国产精品中文字幕 | 亚洲国产成人久久一区久久| 久久久久久亚洲精品无码| 久久婷婷五月综合成人D啪| 久久这里的只有是精品23| 亚洲欧美成人综合久久久| 97超级碰碰碰久久久久| 久久久精品国产Sm最大网站| 欧美激情一区二区久久久| 久久婷婷五月综合国产尤物app| 国产一区二区三区久久| 亚洲国产成人精品久久久国产成人一区二区三区综 | 99国产欧美精品久久久蜜芽| 国内精品伊人久久久久影院对白 | 国产V综合V亚洲欧美久久| 色8激情欧美成人久久综合电| 久久久国产乱子伦精品作者| 久久国产成人| 亚洲国产精品久久66| 久久综合噜噜激激的五月天| 日本精品久久久久影院日本| 97久久精品人人做人人爽| 一本色道久久88精品综合| 久久久久久国产精品美女| 国产精品美女久久久久网| 久久亚洲日韩精品一区二区三区| 久久无码国产| 7国产欧美日韩综合天堂中文久久久久 | 亚洲人成网亚洲欧洲无码久久| 精品久久久久久久久久久久久久久| 香蕉久久av一区二区三区| 欧美伊人久久大香线蕉综合| 欧美午夜精品久久久久久浪潮| 亚洲综合精品香蕉久久网97 | 久久99免费视频| …久久精品99久久香蕉国产| 亚洲AV无码1区2区久久| 久久久久国产精品熟女影院| 色综合久久久久久久久五月| 久久久亚洲欧洲日产国码aⅴ| 亚洲AV日韩AV永久无码久久|