• <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毛片免费观看不卡| 久久高潮一级毛片免费| 亚洲精品综合久久| 国产精品99精品久久免费| 久久精品人妻一区二区三区| 久久精品国产男包| 日本久久久久久中文字幕| 久久人人爽人人澡人人高潮AV| 2021国内久久精品| 国产A级毛片久久久精品毛片| 国内精品久久久久影院老司| 国产精品久久久久久久久免费| 日韩欧美亚洲国产精品字幕久久久| 亚洲AV日韩精品久久久久久久| 久久久久久久久久久免费精品| 无码AV中文字幕久久专区| 久久精品国产一区二区| AV色综合久久天堂AV色综合在 | 色欲综合久久躁天天躁| 性高朝久久久久久久久久| 欧美精品丝袜久久久中文字幕 | 日韩久久无码免费毛片软件| 精品综合久久久久久888蜜芽| 三级三级久久三级久久| 久久人人爽人人爽人人片AV麻豆| 99精品久久精品| 久久久久亚洲AV无码麻豆| 久久亚洲sm情趣捆绑调教| 久久国产成人亚洲精品影院| 久久精品国产一区二区三区日韩| 一本久久a久久精品亚洲| 7777精品伊人久久久大香线蕉| 欧洲性大片xxxxx久久久| 久久精品成人免费国产片小草 | 国产农村妇女毛片精品久久| 欧美亚洲另类久久综合| 99re这里只有精品热久久| 国内精品久久久久久野外|