• <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.線程的終止和產(chǎn)生
            小例:
            #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來創(chuàng)建線程
                其第一個參數(shù)為線程id,第二個為線程屬性,第三個為線程函數(shù),最后一個為數(shù)據(jù)參數(shù)

            那么線程如何終止呢:
            1.從運行函數(shù)終止
            2.調(diào)用pthread_exit終止
            3.調(diào)用pthread_cance
            4.進程終止

            向線程傳遞參數(shù)
            #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.原生態(tài)的平臺線程函數(shù)

            posted on 2010-12-01 14:02 ccsdu2009 閱讀(1810) 評論(0)  編輯 收藏 引用
             
            久久成人国产精品二三区| 中文字幕一区二区三区久久网站| 久久久久国产精品嫩草影院| 青青草原精品99久久精品66| 国产精品久久久久久久久久免费| 久久综合精品国产一区二区三区| 伊人久久大香线蕉综合Av| 青青青国产成人久久111网站| 中文字幕无码av激情不卡久久| 国产综合久久久久久鬼色| 日韩va亚洲va欧美va久久| 久久精品人人做人人爽97| 亚洲欧洲精品成人久久奇米网| 久久99精品久久久久婷婷| 久久综合偷偷噜噜噜色| 99久久国产亚洲高清观看2024 | 伊人久久久AV老熟妇色| 精品熟女少妇aⅴ免费久久| 亚洲精品无码久久一线| 亚洲成av人片不卡无码久久| 中文字幕亚洲综合久久| 91久久精品91久久性色| 亚洲色大成网站www久久九| 亚洲人成无码久久电影网站| 久久激情亚洲精品无码?V| 久久最近最新中文字幕大全| 97久久香蕉国产线看观看| 亚洲国产精品无码久久久秋霞2 | 亚州日韩精品专区久久久| 国产呻吟久久久久久久92| 久久―日本道色综合久久| 潮喷大喷水系列无码久久精品| 久久久久99这里有精品10| 亚洲人成网站999久久久综合| 亚洲国产成人乱码精品女人久久久不卡 | 伊人久久大香线蕉av不变影院| 伊人久久无码精品中文字幕| 四虎影视久久久免费观看| 亚洲第一永久AV网站久久精品男人的天堂AV | 久久精品亚洲中文字幕无码麻豆 | 国内精品久久久久久久影视麻豆|