小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

pthread

 棄哲 2011-06-10

pthread_create

編輯本段函數(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,否則返回出錯編號
  返回成功時,由tidp指向的內(nèi)存單元被設置為新創(chuàng)建線程的線程ID。attr參數(shù)用于制定各種不同的線程屬性。新創(chuàng)建的線程從start_rtn函數(shù)的地址開始運行,該函數(shù)只有一個空指針參數(shù)arg,如果需要向start_rtn函數(shù)傳遞的參數(shù)不止一個,那么需要把這些參數(shù)放到一個結(jié)構(gòu)中,然后把這個結(jié)構(gòu)的地址作為arg的參數(shù)傳入。
  linux下用C開發(fā)多線程程序,Linux系統(tǒng)下的多線程遵循POSIX線程接口,稱為pthread。
  由 restrict 修飾的指針是最初唯一對指針所指向的對象進行存取的方法,僅當?shù)诙€指針基于第一個時,才能對對象進行存取。對對象的存取都限定于基于由 restrict 修飾的指針表達式中。 由 restrict 修飾的指針主要用于函數(shù)形參,或指向由 malloc() 分配的內(nèi)存空間。restrict 數(shù)據(jù)類型不改變程序的語義。 編譯器能通過作出 restrict 修飾的指針是存取對象的唯一方法的假設,更好地優(yōu)化某些類型的例程。

參數(shù)

  第一個參數(shù)為指向線程標識符的指針。
  第二個參數(shù)用來設置線程屬性。
  第三個參數(shù)是線程運行函數(shù)的起始地址。
  最后一個參數(shù)是運行函數(shù)的參數(shù)。
  另外,在編譯時注意加上-lpthread參數(shù),以調(diào)用靜態(tài)鏈接庫。因為pthread并非Linux系統(tǒng)的默認庫

示例

  打印線程 IDs
  #include "apue.h"
  #include <pthread.h>
  pthread_t ntid;
  void printids(const char *s)
  {
  pid_t pid;
  pthread_t tid;
  pid = getpid();
  tid = pthread_self();
  printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int)pid, (unsigned int)tid, (unsigned int)tid);
  }
  void *thr_fn(void *arg)
  {
  printids("new thread: ");
  return((void *)0);
  }
  intmain(void)
  {
  int err;
  err = pthread_create(&ntid, NULL, thr_fn, NULL);
  if (err != 0)
  err_quit("can't create thread: %s\n", strerror(err));
  printids("main thread:");
  sleep(1);
  exit(0);
  }

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多