• <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)  編輯 收藏 引用
             
            蜜臀久久99精品久久久久久小说| AV色综合久久天堂AV色综合在| 99久久精品无码一区二区毛片 | 99久久人人爽亚洲精品美女| 东京热TOKYO综合久久精品| 久久人人爽人人爽人人AV东京热 | 久久91精品国产91久久小草| 久久99精品久久久久久水蜜桃| 欧美va久久久噜噜噜久久| 嫩草影院久久99| 99久久精品免费看国产一区二区三区 | 日本人妻丰满熟妇久久久久久| 国产精品久久久久久久久免费| 久久久精品人妻一区二区三区蜜桃| 91精品免费久久久久久久久| 亚洲国产精品成人AV无码久久综合影院| 久久精品水蜜桃av综合天堂| 久久婷婷色综合一区二区| 99久久婷婷国产综合亚洲| 四虎国产精品成人免费久久| 成人精品一区二区久久| 久久精品国产精品国产精品污| 久久久久亚洲?V成人无码| 国内精品久久久久久久coent| 久久伊人精品一区二区三区 | 精品多毛少妇人妻AV免费久久| 77777亚洲午夜久久多人| 久久无码AV中文出轨人妻| 狠狠精品久久久无码中文字幕 | 精品久久亚洲中文无码| 久久se精品一区精品二区国产| 久久青青草原精品国产| 伊人久久大香线蕉av不变影院| 久久久久久久综合日本| 99久久国产免费福利| 久久99国产精品久久久| 精品蜜臀久久久久99网站| 久久婷婷国产综合精品| 婷婷久久久亚洲欧洲日产国码AV| 浪潮AV色综合久久天堂| 丁香色欲久久久久久综合网|