• <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++高級(jí)工程師 Android高級(jí)軟件工程師 IT集成工程師 音頻工程師 熟悉c,c++,java,c#,py,js,asp等多種語言 程序猿
            1.使用pthread的理由
                  2個(gè)字:簡(jiǎn)單
            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)建線程
                其第一個(gè)參數(shù)為線程id,第二個(gè)為線程屬性,第三個(gè)為線程函數(shù),最后一個(gè)為數(shù)據(jù)參數(shù)

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

            向線程傳遞參數(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;
            }

            其他相關(guān)線程庫:
            1.zthread,
            2.opentherad
            3.boost therad
            4.原生態(tài)的平臺(tái)線程函數(shù)

            posted on 2010-12-01 14:02 ccsdu2009 閱讀(1809) 評(píng)論(0)  編輯 收藏 引用
             
            蜜臀av性久久久久蜜臀aⅴ| 久久不见久久见免费视频7| 久久久久免费精品国产| 午夜福利91久久福利| 亚洲欧美日韩久久精品| 久久亚洲国产欧洲精品一| 国产精品gz久久久| 精品久久无码中文字幕| 中文字幕久久波多野结衣av| 亚洲va久久久噜噜噜久久男同| 性做久久久久久久久浪潮| 伊人伊成久久人综合网777| 久久国产高清字幕中文| 中文成人久久久久影院免费观看| 一级做a爱片久久毛片| 久久精品日日躁夜夜躁欧美| 亚洲精品第一综合99久久| 色狠狠久久综合网| 91久久精品91久久性色| 久久精品国产亚洲av瑜伽| 国内精品九九久久久精品| 久久亚洲私人国产精品| 国产A级毛片久久久精品毛片| 亚洲乱码日产精品a级毛片久久 | 亚洲综合久久综合激情久久| 国产毛片久久久久久国产毛片| 亚洲国产成人久久综合一| 伊人久久免费视频| 亚洲精品午夜国产va久久| 久久久久99这里有精品10| 国产精品久久久久久久| 亚洲欧美国产日韩综合久久| 久久国产热精品波多野结衣AV| 欧美精品丝袜久久久中文字幕| 天天综合久久久网| 香蕉久久夜色精品国产2020| 色88久久久久高潮综合影院| 久久综合久久自在自线精品自| 日本一区精品久久久久影院| 蜜桃麻豆www久久国产精品| 久久久久99精品成人片牛牛影视|