• <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综合合色蜜桃网| 国产精品热久久毛片| 亚洲国产精品嫩草影院久久| 精品多毛少妇人妻AV免费久久| 国产精品一区二区久久精品无码| 噜噜噜色噜噜噜久久| 人妻久久久一区二区三区| 久久国产美女免费观看精品| 亚洲国产美女精品久久久久∴| 亚洲精品国产自在久久| 久久精品国产亚洲av麻豆色欲| 大香伊人久久精品一区二区 | A级毛片无码久久精品免费| 国产精品久久一区二区三区 | 欧美激情精品久久久久久久九九九| 久久水蜜桃亚洲av无码精品麻豆| 色播久久人人爽人人爽人人片aV| 久久夜色tv网站| 97久久天天综合色天天综合色hd| 日日躁夜夜躁狠狠久久AV| 国产精品VIDEOSSEX久久发布| 国内精品人妻无码久久久影院 | 久久中文字幕精品| 国产99久久久国产精品~~牛| 精品久久久久久国产潘金莲| 中文字幕无码久久久| 日本国产精品久久| 久久天天躁狠狠躁夜夜av浪潮| 国产99久久久国产精免费| 久久精品这里热有精品| 国产精品无码久久久久久| 国产成人久久精品激情| 久久久精品国产sm调教网站| 人妻无码久久一区二区三区免费| 7777久久亚洲中文字幕| 97久久精品无码一区二区天美|