• <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精品国产自在现线小黄鸭| 国产偷久久久精品专区| 91久久国产视频| 香蕉久久夜色精品升级完成| 精品久久久久久久| 热久久最新网站获取| 久久久久久久综合日本亚洲| 亚洲国产成人久久综合区| 国内精品久久久久伊人av| 久久精品?ⅴ无码中文字幕| 亚洲AV无码一区东京热久久| 久久e热在这里只有国产中文精品99| 2021国产精品午夜久久| 亚洲国产精品热久久| 久久精品蜜芽亚洲国产AV| 久久经典免费视频| 国产精品久久久久久久久久免费| 久久水蜜桃亚洲av无码精品麻豆| 午夜精品久久久久久| 国产免费久久精品99久久| 伊人热人久久中文字幕| 久久99国产精品尤物| 国产成人无码精品久久久性色 | AV无码久久久久不卡网站下载| 亚洲精品成人久久久| 亚洲AⅤ优女AV综合久久久| 久久WWW免费人成—看片| 91麻豆精品国产91久久久久久| 国产成人久久精品一区二区三区 | 伊人久久大香线蕉综合Av| 亚洲精品国产综合久久一线| 国产精品熟女福利久久AV| 久久国产精品免费一区| 久久久久久av无码免费看大片| 国产精品成人无码久久久久久 | av无码久久久久不卡免费网站| 中文国产成人精品久久不卡| 久久AV高潮AV无码AV| 亚洲国产精品无码久久98|