• <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)  編輯 收藏 引用
             
            97久久精品人人澡人人爽| 久久久久久久尹人综合网亚洲| 久久精品国产亚洲Aⅴ蜜臀色欲 | av色综合久久天堂av色综合在| 久久精品国产免费观看| 久久91综合国产91久久精品| AV无码久久久久不卡网站下载| 久久国产一片免费观看| 久久综合狠狠综合久久| 久久久久人妻一区精品果冻| 久久国产乱子伦免费精品| 久久久久一级精品亚洲国产成人综合AV区| 国产成人无码精品久久久性色| 日本免费一区二区久久人人澡| 久久精品日日躁夜夜躁欧美| 久久91这里精品国产2020| 色欲综合久久中文字幕网| 日批日出水久久亚洲精品tv| 91麻豆精品国产91久久久久久| 久久久亚洲AV波多野结衣| 久久人人超碰精品CAOPOREN| 九九99精品久久久久久| 久久久久人妻精品一区二区三区| 久久久久久国产a免费观看不卡 | 久久久久亚洲AV无码网站| 亚洲国产成人久久综合野外| 精品一久久香蕉国产线看播放| 国产精品久久久久久久久鸭| 久久精品国产亚洲AV无码娇色| 国产成人久久精品一区二区三区| 伊人久久成人成综合网222| 青青久久精品国产免费看| 久久精品中文字幕有码| 久久久精品午夜免费不卡| 久久精品99久久香蕉国产色戒| 久久久久亚洲av无码专区导航| 少妇久久久久久被弄高潮| 久久国产亚洲精品无码| 伊人久久精品线影院| 久久精品国产亚洲精品| 午夜视频久久久久一区|