• <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>
            Cpper
            C/C++高級工程師 Android高級軟件工程師 IT集成工程師 音頻工程師 熟悉c,c++,java,c#,py,js,asp等多種語言 程序猿
            1.使用pthread的理由
                  2個字:簡單
            2.pthread組件
                  thread,
                  mutex,     
                  condition var
                  synchronization
            3.線程的終止和產生
            小例:
            #include <pthread/pthread.h>
            #include <stdio.h>
            #define NUM_THREADS     5

            void *PrintHello(void *threadid)
            {
                  long tid;
                  tid = (long)threadid;
                  printf("Hello World! It's me, thread #%ld!\n", tid);
                  pthread_exit(NULL);
            }

            int main (int argc, char *argv[])
            {
                 pthread_t threads[NUM_THREADS];
                 int rc;
                 long t;
                 for(t=0; t<NUM_THREADS; t++)
                 {
                      printf("In main: creating thread %ld\n", t);
                      rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
                      if (rc)
                      {
                           printf("ERROR; return code from pthread_create() is %d\n", rc);
                           exit(-1);
                      }
                  }
                 pthread_exit(NULL);
                 return 0;
            }

            通過pthread_create來創建線程
                其第一個參數為線程id,第二個為線程屬性,第三個為線程函數,最后一個為數據參數

            那么線程如何終止呢:
            1.從運行函數終止
            2.調用pthread_exit終止
            3.調用pthread_cance
            4.進程終止

            向線程傳遞參數
            #include <pthread/pthread.h>
            #include <stdio.h>
            #include <string>
            #include <iostream>
            #define NUM_THREADS     5

            struct Data
            {
                    std::string name;
            };

            Data* data_impl;

            void *PrintHello(void* data_ptr)
            {
                     struct Data* data;
                     data = (Data*)data_ptr;
                     std::cout<<data->name<<std::endl;
                     pthread_exit(NULL);
            }

            int main (int argc, char *argv[])
            {
                  pthread_t threads[NUM_THREADS];
                  data_impl = new Data[NUM_THREADS];
                  data_impl[0].name = "T1";
                  data_impl[1] .name= "T2";
                  data_impl[2] .name= "T3";
                  data_impl[3] .name= "T4";
                  data_impl[4] .name= "T5";
                  for(int t=0; t<NUM_THREADS; t++)
                  {
                      int rc = pthread_create(&threads[t], NULL, PrintHello,&data_impl[t]);
                  }
                  pthread_exit(NULL);
                  delete []data_impl;
                  return 0;
            }

            其他相關線程庫:
            1.zthread,
            2.opentherad
            3.boost therad
            4.原生態的平臺線程函數

            posted on 2010-12-01 14:02 ccsdu2009 閱讀(1809) 評論(0)  編輯 收藏 引用
             
            热re99久久6国产精品免费| 亚洲一本综合久久| 久久国产成人亚洲精品影院| 久久水蜜桃亚洲av无码精品麻豆| 一本色综合久久| 伊人情人综合成人久久网小说 | 久久亚洲国产午夜精品理论片| 伊人久久大香线蕉亚洲五月天| 国产精品久久久久久久久久影院| 午夜视频久久久久一区| 久久综合久久综合亚洲| 99久久精品免费看国产一区二区三区 | 狠狠色丁香久久婷婷综合图片 | 青青青青久久精品国产| 国产激情久久久久影院小草| 人妻少妇精品久久| 久久99精品久久久大学生| 久久人人爽人人爽人人片AV高清| 午夜天堂精品久久久久| 国产情侣久久久久aⅴ免费| 一本一道久久精品综合| 天堂无码久久综合东京热| 午夜不卡久久精品无码免费| 婷婷综合久久狠狠色99h| 青青青青久久精品国产h久久精品五福影院1421| 久久久精品视频免费观看| av色综合久久天堂av色综合在| 久久免费的精品国产V∧| 99久久精品免费观看国产| 久久妇女高潮几次MBA| 久久久久一区二区三区| 久久人人爽人人爽人人av东京热 | 99久久精品日本一区二区免费| 成人亚洲欧美久久久久| 久久午夜福利无码1000合集| 狠狠狠色丁香婷婷综合久久五月| 久久久这里有精品中文字幕| www.久久99| 久久久噜噜噜久久中文字幕色伊伊| 国产精品久久网| 久久w5ww成w人免费|