溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Linux系統(tǒng)中如何創(chuàng)建線程

發(fā)布時(shí)間:2022-01-25 10:05:03 來源:億速云 閱讀:120 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下Linux系統(tǒng)中如何創(chuàng)建線程,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

線程創(chuàng)建

在傳統(tǒng)Unix進(jìn)程模型中,每個(gè)進(jìn)程只有一個(gè)控制線程。在POSIX線程(pthread)的情況下,程序開始運(yùn)行時(shí),它也是以單進(jìn)程中的單個(gè)控制線程啟動(dòng)的。在創(chuàng)建多個(gè)控制線程以前,程序的行為與傳統(tǒng)的進(jìn)程并沒有什么區(qū)別。新增的線程可以通過調(diào)用pthread_create函數(shù)創(chuàng)建。

 #include 
 int pthread_create(pthread_t *restrict tidp,
        const pthread_attr_t *restrict attr,
        void *(*start_rtn)(void *), void *restrict arg);
 12345

說明:當(dāng)pthread_create成功返回時(shí),新創(chuàng)建線程的線程ID會(huì)被設(shè)置成tidp指向的內(nèi)存單元。attr參數(shù)用于定制各種不同的線程屬性,目前設(shè)置為NULL,創(chuàng)建一個(gè)具有默認(rèn)屬性的線程。 新創(chuàng)建的線程從start_rtn函數(shù)的地址開始運(yùn)行,該函數(shù)只有一個(gè)無類型指針參數(shù)arg。如果需要想start_rtn函數(shù)傳遞的參數(shù)有一個(gè)以上,那么需要把這些參數(shù)放到一個(gè)結(jié)構(gòu)中,然后把這個(gè)結(jié)構(gòu)的地址作為arg參數(shù)傳入。

例子

創(chuàng)建一個(gè)線程,打印進(jìn)程ID、新線程的線程ID以及初始線程的ID。

 //gcc threadid.c -o a.out -pthread
 //pthread是linux下的線程庫,用了多線程就要鏈接這個(gè)庫,這時(shí)候要在編譯選項(xiàng)上增加-pthread
 #include "apue.h"
 #include #include 
 
 pthread_t ntid;
 
 void
 printids(const char *s)
 {
  //聲明進(jìn)程id
  pid_t  pid;
  //聲明線程id
  pthread_t tid;
  //獲取進(jìn)程id
  pid = getpid();
  //用pthread_self()獲取自己線程id
  tid = pthread_self();
  printf("%s pid %lu tid %lu (0x%lx)\n", s, (unsigned long)pid,
    (unsigned long)tid, (unsigned long)tid);
 }
 
 void *
 thr_fn(void *arg)
 {
  //調(diào)用上面的打印id函數(shù)
  printids("new thread: ");
  return((void *)0);
 }
 
 int
 main(void)
 {
  int  err;
 
  //創(chuàng)建線程,主線程把新線程ID存放在ntid中,新線程去執(zhí)行thr_fn函數(shù)
  err = pthread_create(&ntid, NULL, thr_fn, NULL);
  if (err != 0)
   err_exit(err, "can't create thread");
  printids("main thread:");
  sleep(1);
  exit(0);
 }
 1234567891011121314151617181920212223242526272829303132333435363738394041424344

編譯: gcc threadid.c -o a.out -pthread note:pthread是linux下的線程庫,用了多線程就要鏈接這個(gè)庫,這時(shí)候要在編譯選項(xiàng)上增加-pthread

 main thread: pid 1518 tid 140627397551936 (0x7fe65e13a740)
 new thread:  pid 1518 tid 140627389191936 (0x7fe65d941700)
 12

可以見到,兩個(gè)線程地址是不一樣的,但是pid父進(jìn)程都是一樣的。

另一個(gè)例子

主線程接受一個(gè)輸入num,創(chuàng)建一個(gè)線程打印2*num。

 #include #include #include #include 
 
 // 返回值必須是void *(無類型指針),參數(shù)也必須是void *
 void *tfn(void *arg)
 {
     //傳進(jìn)來的是(void *),強(qiáng)制類型轉(zhuǎn)換
     int num = (int)arg;
     long d2num = 2*num;
     printf("In thread %lu arg: %d.\n", (unsigned long)pthread_self(), num);
     sleep(1);
     printf("Thread over! arg: %d\n", num);
     return  (void*)d2num;
 }
 
 void main(int argc, char *argv[])
 {
     pthread_t tid;
     long num;
     void *tret; //獲取線程終止?fàn)顟B(tài)
     while(scanf("%ld", &num) == 1){
         pthread_create(&tid, NULL, tfn, (void *)num);
         //線程終止
         pthread_join(tid, &tret);
         printf("Thread exit code: %ld\n", (long)tret);
     }
 
     printf("Main thread %lu is over.\n", (unsigned long)pthread_self());
 }
 12345678910111213141516171819202122232425262728293031

因?yàn)樵赨nix環(huán)境高級編程的源代碼里代碼都是使用makefile鏈接的,小弟不懂,所以直接將代碼copy到已有的文件名中跑了。 下面是結(jié)果: Linux系統(tǒng)中如何創(chuàng)建線程 可以看到結(jié)果:創(chuàng)建的線程和主線程id號(hào)是不一樣的。 另外:我覺的在 pthread_create(&tid, NULL, tfn, (void *)num); 直接將num轉(zhuǎn)為指針有點(diǎn)別扭,于是這樣: pthread_create(&tid, NULL, tfn, (void *)&num); 然后在tfn函數(shù)中將: int num = (int)arg; 改為: int num = *(int*)arg; 也是可以的。

以上是“Linux系統(tǒng)中如何創(chuàng)建線程”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI