濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > Linux中多線程詳解及簡(jiǎn)單實(shí)例

Linux中多線程詳解及簡(jiǎn)單實(shí)例

熱門(mén)標(biāo)簽:昆明電銷(xiāo)機(jī)器人價(jià)格 400的電話一般從哪里辦理 江西全自動(dòng)外呼系統(tǒng)報(bào)價(jià) 益陽(yáng)400電話申請(qǐng)辦理流程 春運(yùn)地圖標(biāo)注app 怎么用百度地圖標(biāo)注坐標(biāo) 地圖標(biāo)注人員分布 梧州防封電銷(xiāo)卡 上海機(jī)器人外呼系統(tǒng)哪家好

Linux中多線程詳解及簡(jiǎn)單實(shí)例

1.概念

進(jìn)程:運(yùn)行中的程序。

線程:一個(gè)程序中的多個(gè)執(zhí)行路徑。更準(zhǔn)確的定義是:線程是一個(gè)進(jìn)程內(nèi)部的一個(gè)控制序列。

2.為什么要有線程?

用fork調(diào)用進(jìn)程代價(jià)太高,需要讓一個(gè)進(jìn)程同時(shí)做多件事情,線程就非常有用。

3.線程的優(yōu)點(diǎn)和缺點(diǎn)。

優(yōu)點(diǎn):

(1)有時(shí),讓程序看起來(lái)是在同時(shí)做兩件事是非常有用的。 比如在編輯文檔時(shí),還能統(tǒng)計(jì)文檔里的單詞個(gè)數(shù)。
(2)一個(gè)混雜著輸入、計(jì)算、輸出的程序,利用線程可以將這3個(gè)部 分分成3個(gè)線程來(lái)執(zhí)行,從而改變程序執(zhí)行的性能。
(3)一般來(lái)說(shuō),線程之間切換需要操作系統(tǒng)所做的工作比進(jìn)程間切換需要的代價(jià)小。

缺點(diǎn):

(1)編寫(xiě)線程需要非常仔細(xì)的設(shè)計(jì)。
(2)對(duì)多線程的調(diào)試?yán)щy程度比單線程調(diào)試大得多。

4.創(chuàng)建線程

#include pthread.h>
(1)int pthread_create(pthread_t *thread,pthread_attr_t *attr,void *(*start_routine)(void *),void *arg);
pthread_t pthread_self(void);
(2)int pthread_equal(pthread_t thread1,pthread_t thread2);
(3)int pthread_once(pthread_once_t *once_control,void(*init_routine)(void));

Linux系統(tǒng)支持POSIX多線程接口,稱(chēng)為pthread。編寫(xiě)linux下的多線程程序,需要包含頭文件pthread.h,鏈接時(shí)需要使用庫(kù)libpthread.a。

如果在主線程里面創(chuàng)建線程,程序就會(huì)在創(chuàng)建線程的地方產(chǎn)生分支,變成兩個(gè)部分執(zhí)行。線程的創(chuàng)建通過(guò)函數(shù)pthread_create來(lái)完成。成功返回0。

1.線程創(chuàng)建: 
int pthread_create(pthread_t thread,pthread_attr_t *attr,void (start_routine)(void ),void *arg); 
pthread_t pthread_self(void); 
參數(shù)說(shuō)明: 
thread:指向pthread_create類(lèi)型的指針,用于引用新創(chuàng)建的線程。 
attr:用于設(shè)置線程的屬性,一般不需要特殊的屬性,所以可以簡(jiǎn)單地設(shè)置為NULL。 
(start_routine)(void ):傳遞新線程所要執(zhí)行的函數(shù)地址。 
arg:新線程所要執(zhí)行的函數(shù)的參數(shù)。 
調(diào)用如果成功,則返回值是0,如果失敗則返回錯(cuò)誤代碼。 
2.線程終止 
void pthread_exit(void *retval); 
參數(shù)說(shuō)明: 
retval:返回指針,指向線程向要返回的某個(gè)對(duì)象。 
線程通過(guò)調(diào)用pthread_exit函數(shù)終止執(zhí)行,并返回一個(gè)指向某對(duì)象的指針。注意:絕不能用它返回一個(gè)指向局部變量的指針,因?yàn)榫€程調(diào)用該函數(shù)后,這個(gè)局部變量就不存在了,這將引起嚴(yán)重的程序漏洞。 
3.線程同步 
int pthread_join(pthread_t th, void **thread_return); 
參數(shù)說(shuō)明: 
th:將要等待的線程,線程通過(guò)pthread_create返回的標(biāo)識(shí)符來(lái)指定。 
thread_return:一個(gè)指針,指向另一個(gè)指針,而后者指向線程的返回值。 

一個(gè)簡(jiǎn)單的創(chuàng)建多線程的程序:

#include pthread.h>
#include stdio.h>
#include stdlib.h>
#include string.h>

void *thread_function(void *arg);

char message[] = "Hello World";

int main()
{
  int res;
  pthread_t a_thread;
  void *thread_result;

  res = pthread_create(a_thread, NULL, thread_function, (void *)message);
  if (res != 0)
  {
    perror("Thread creation failed!");
    exit(EXIT_FAILURE);
  }

  printf("Waiting for thread to finish.../n");

  res = pthread_join(a_thread, thread_result);
  if (res != 0)
  {
    perror("Thread join failed!/n");
    exit(EXIT_FAILURE);
  }

  printf("Thread joined, it returned %s/n", (char *)thread_result);
  printf("Message is now %s/n", message);

  exit(EXIT_FAILURE);
}

void *thread_function(void *arg)
{
  printf("thread_function is running. Argument was %s/n", (char *)arg);
  sleep(3);
  strcpy(message, "Bye!");
  pthread_exit("Thank you for your CPU time!");
}

輸出結(jié)果

$./thread1[輸出]:
thread_function is running. Argument was Hello World
Waiting for thread to finish...
Thread joined, it returned Thank you for your CPU time!
Message is now Bye!

以上就是Linux 多線程的實(shí)例詳解,如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

您可能感興趣的文章:
  • Linux多線程編程快速入門(mén)
  • linux下c語(yǔ)言的多線程編程
  • linux下的C\C++多進(jìn)程多線程編程實(shí)例詳解
  • 詳解Linux多線程編程(不限Linux)
  • linux多線程編程(五)
  • linux多線程編程(四)
  • Linux下的多線程編程(三)
  • Linux多線程編程(二)
  • Linux多線程編程(一)
  • linux多線程編程詳解教程(線程通過(guò)信號(hào)量實(shí)現(xiàn)通信代碼)
  • Linux下的多線程編程實(shí)例解析

標(biāo)簽:惠州 河南 九江 贛州 亳州 新疆 懷化 北京

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Linux中多線程詳解及簡(jiǎn)單實(shí)例》,本文關(guān)鍵詞  Linux,中多,線程,詳解,及,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Linux中多線程詳解及簡(jiǎn)單實(shí)例》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Linux中多線程詳解及簡(jiǎn)單實(shí)例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    波密县| 南京市| 衡南县| 海伦市| 应城市| 茂名市| 明溪县| 哈尔滨市| 绿春县| 行唐县| 武清区| 宁国市| 长乐市| 晋中市| 长宁县| 张家港市| 武清区| 巴东县| 叶城县| 通江县| 枣强县| 镇平县| 玉门市| 肥城市| 民勤县| 高安市| 蒙阴县| 枣阳市| 仁怀市| 富川| 泰来县| 阿拉善右旗| 治多县| 天祝| 剑川县| 本溪| 齐齐哈尔市| 泽普县| 连城县| 江津市| 吉林省|