• <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.線程的終止和產生
            小例:
            #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.調用pthread_exit終止
            3.調用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 閱讀(1806) 評論(0)  編輯 收藏 引用
             
            亚洲人成无码网站久久99热国产 | 国产99精品久久| 久久天天躁狠狠躁夜夜96流白浆| 欧美黑人激情性久久| 久久成人精品视频| 伊人色综合九久久天天蜜桃| 中文无码久久精品| 精品久久久无码中文字幕| 亚洲天堂久久久| 国产精品99久久久久久董美香| 亚洲色欲久久久久综合网 | 青青青伊人色综合久久| 久久五月精品中文字幕| 久久精品夜夜夜夜夜久久| 狠狠综合久久综合中文88| 久久久久亚洲av无码专区喷水| 久久久久国色AV免费观看| 麻豆一区二区99久久久久| 久久精品桃花综合| 久久人人爽人人澡人人高潮AV | 久久精品国产清自在天天线 | 欧美va久久久噜噜噜久久| 久久亚洲2019中文字幕| 久久福利青草精品资源站免费| 久久亚洲国产成人影院| 亚洲国产精品嫩草影院久久| 欧美一区二区精品久久| 国产精品久久久久久福利漫画| 色妞色综合久久夜夜| 亚洲精品乱码久久久久久按摩| 久久精品极品盛宴观看| 亚洲日本久久久午夜精品| 一本色道久久综合| 久久久亚洲欧洲日产国码是AV| 久久99热这里只有精品国产 | 国产亚洲精午夜久久久久久| 国产精品美女久久久m| 久久99国产综合精品| 99精品国产在热久久无毒不卡| 精品午夜久久福利大片| 91精品国产91久久久久久青草|