濮阳杆衣贸易有限公司

主頁 > 知識庫 > linux創(chuàng)建線程之pthread_create的具體使用

linux創(chuàng)建線程之pthread_create的具體使用

熱門標(biāo)簽:地圖標(biāo)注需要現(xiàn)場嗎 鶴壁電銷外呼系統(tǒng)怎么安裝 地圖標(biāo)注企業(yè)名稱侵權(quán)案件 繽客網(wǎng)注冊時地圖標(biāo)注出不來 工廠位置地圖標(biāo)注 企業(yè)400電話辦理哪正規(guī) 400電話辦理哪家好廠商 網(wǎng)站上插入地圖標(biāo)注內(nèi)容 重慶營銷外呼系統(tǒng)排名

pthread_create函數(shù)

函數(shù)簡介

  pthread_create是UNIX環(huán)境創(chuàng)建線程函數(shù)

頭文件

  #include<pthread.h>

函數(shù)聲明

  int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict_attr,void*(*start_rtn)(void*),void *restrict arg);

返回值

  若成功則返回0,否則返回出錯編號

參數(shù)

  第一個參數(shù)為指向線程標(biāo)識符的指針。

  第二個參數(shù)用來設(shè)置線程屬性。

  第三個參數(shù)是線程運行函數(shù)的地址。

  最后一個參數(shù)是運行函數(shù)的參數(shù)。

注意

  在編譯時注意加上-lpthread參數(shù),以調(diào)用靜態(tài)鏈接庫。因為pthread并非Linux系統(tǒng)的默認庫。

pthread_join函數(shù)

函數(shù)簡介

  函數(shù)pthread_join用來等待一個線程的結(jié)束。

函數(shù)原型為:

  extern int pthread_join __P (pthread_t __th, void **__thread_return);

參數(shù):

  第一個參數(shù)為被等待的線程標(biāo)識符

  第二個參數(shù)為一個用戶定義的指針,它可以用來存儲被等待線程的返回值。

注意

    這個函數(shù)是一個線程阻塞的函數(shù),調(diào)用它的函數(shù)將一直等待到被等待的線程結(jié)束為止,當(dāng)函數(shù)返回時,被等待線程的資源被收回。如果執(zhí)行成功,將返回0,如果失敗則返回一個錯誤號。

例子:

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

/* 聲明結(jié)構(gòu)體 */
struct member
{
  int num;
  char *name;
};

/* 定義線程pthread */
static void * pthread(void *arg)
{
  struct member *temp;

  /* 線程pthread開始運行 */
  printf("pthread start!\n");

  /* 令主線程繼續(xù)執(zhí)行 */
  sleep(2);

  /* 打印傳入?yún)?shù) */
  temp = (struct member *)arg;
  printf("member->num:%d\n",temp->num);
  printf("member->name:%s\n",temp->name);

  return NULL;
}

/* main函數(shù) */
int main(int agrc,char* argv[])
{
  pthread_t tidp;
  struct member *b;

  /* 為結(jié)構(gòu)體變量b賦值 */
  b = (struct member *)malloc(sizeof(struct member));
  b->num=1;
  b->name="mlq";

  /* 創(chuàng)建線程pthread */
  if ((pthread_create(&tidp, NULL, pthread, (void*)b)) == -1)
  {
    printf("create error!\n");
    return 1;
  }

  /* 令線程pthread先運行 */
  sleep(1);

  /* 線程pthread睡眠2s,此時main可以先執(zhí)行 */
  printf("mian continue!\n");

  /* 等待線程pthread釋放 */
  if (pthread_join(tidp, NULL))
  {
    printf("thread is not exit...\n");
    return -2;
  }

  return 0;
}

編譯與執(zhí)行結(jié)果

    編譯與執(zhí)行結(jié)果如下圖所示,可以看到主線程main和線程pthread交替執(zhí)行。也就是說是當(dāng)我們創(chuàng)建了線程pthread之后,兩個線程都在執(zhí)行,證明創(chuàng)建成功。另外,可以看到創(chuàng)建線程pthread時候,傳入的參數(shù)被正確打印。


到此這篇關(guān)于linux創(chuàng)建線程之pthread_create的具體使用的文章就介紹到這了,更多相關(guān)linux pthread_create內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

標(biāo)簽:鹽城 東莞 棗莊 96 克拉瑪依 常州 日照 渭南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《linux創(chuàng)建線程之pthread_create的具體使用》,本文關(guān)鍵詞  linux,創(chuàng)建,線程,之,pthread,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《linux創(chuàng)建線程之pthread_create的具體使用》相關(guān)的同類信息!
  • 本頁收集關(guān)于linux創(chuàng)建線程之pthread_create的具體使用的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    滨海县| 丹江口市| 宁波市| 新兴县| 湘乡市| 临清市| 阿城市| 镇坪县| 尉氏县| 宁夏| 玛沁县| 凉城县| 台北市| 松滋市| 油尖旺区| 泰和县| 尼玛县| 桃园县| 长宁区| 萍乡市| 潮安县| 田林县| 宜都市| 龙川县| 龙口市| 沾化县| 舒城县| 土默特右旗| 吴川市| 大悟县| 河北区| 广水市| 宁波市| 普格县| 兴化市| 巴楚县| 阿拉善左旗| 平利县| 康平县| 文水县| 余干县|