• <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等多種語(yǔ)言 程序猿
            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;
            }

            通過(guò)pthread_create來(lái)創(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)線程庫(kù):
            1.zthread,
            2.opentherad
            3.boost therad
            4.原生態(tài)的平臺(tái)線程函數(shù)

            posted on 2010-12-01 14:02 ccsdu2009 閱讀(1806) 評(píng)論(0)  編輯 收藏 引用
             
            色综合久久久久| 中文字幕精品久久久久人妻| 性高湖久久久久久久久| 亚洲AV无码久久精品狠狠爱浪潮| 久久无码中文字幕东京热| 欧美一区二区三区久久综| 久久夜色tv网站| 国产aⅴ激情无码久久| 2021久久精品国产99国产精品| 久久精品一区二区影院| 久久久久亚洲av无码专区喷水 | 国产精品美女久久久久久2018| 久久香蕉国产线看观看乱码| 噜噜噜色噜噜噜久久| 热久久国产精品| 精品久久亚洲中文无码| 久久99精品久久久久久齐齐| 久久综合狠狠综合久久| 久久综合亚洲色HEZYO国产| 丁香狠狠色婷婷久久综合| 一本色道久久88综合日韩精品 | 日本三级久久网| 久久久久久久人妻无码中文字幕爆| 久久精品国产亚洲Aⅴ香蕉| WWW婷婷AV久久久影片| 久久久久久久精品成人热色戒| 国产亚洲美女精品久久久| 2020最新久久久视精品爱| 国内精品人妻无码久久久影院| 777午夜精品久久av蜜臀| 77777亚洲午夜久久多人| 亚洲国产精品狼友中文久久久| 亚洲国产精品热久久| 久久亚洲国产午夜精品理论片 | 伊人久久亚洲综合影院| 日韩十八禁一区二区久久| 精品一久久香蕉国产线看播放| 久久精品嫩草影院| 国产一久久香蕉国产线看观看| 久久久国产精品网站| 国产福利电影一区二区三区久久久久成人精品综合 |