青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Prayer

在一般中尋求卓越
posts - 1256, comments - 190, trackbacks - 0, articles - 0
  C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

linux 線程函數大全

Posted on 2009-09-03 16:32 Prayer 閱讀(8632) 評論(1)  編輯 收藏 引用 所屬分類: LINUX/UNIX/AIX

線程

創建一個缺省的線程

缺省的線程的屬性:

l         非綁定

l         未分離

l         一個缺省大小的堆棧

l         具有和父線程一樣的優先級

 

用 phread_attr_init() 創建一個缺省的屬性對象,

用屬性對象創建一個線程 pthread_create(3T)

 

int p thread_create ( pthread_t *tid, const pthread_attr_t *tattr, void *(*start_routine)(void*), void *arg );

 

#include<pthread.h>

pthread_attr_t  tattr;

pthread_t  tid;

extern  void  *start_routine(void *arg);

void  *arg;

int  ret;

 

/*default behavior*/

ret = pthread_create( &tid, NULL, start_routine, arg );

 

/*init with default attributes*/

ret = pthread_attr_init( &tattr );

 

/*default behavior specified*/

ret = pthread_create( &tid, &tattr, start_routine, arg );

 

tattr 中含有初始化線程所需的屬性,值賦為 NULL 即缺省。 start_routine 是線程入口函數的起始地址。當 start_routine 返回時,相應的線程就結束了。線程結束時的退出狀態值是 start_routine 函數用 phread_exit() 函數返回的返回值。當 pthread_create() 函數調用成功時,線程標識符保存在參數 tid 指針指向中。

返回值, pthread_create() 成功后返回 0 ,

EAGAIN 超過了某個限制,如 LWPs 過多。

EINVAL   tattr 值非法。

 

創建子線程時,傳給子線程的輸入參數最好是 malloc() 返回的指針(這樣的指針指向進程堆中的存儲空間)或指向全局變量的指針,而不要是指向局部變量的指針。因為當子線程訪問輸入參數時,創建子線程的函數可能已結束,局部變量也就不存在了。

 

等待線程結束

       pthread_join(3T)

       int pthread_join( pthread_t tid, void **status );

       #include<pthread.h>

      

       pthread_t  tid;

       int  ret;

       int  status;

 

       /*waiting to join thread “tid” with status*/

       ret = pthread_join( tid, &status );

      

       /*waiting to join thread “tid” without status*/

       ret = pthread_join( tid, NULL );

      

       pthread_join() 會阻塞調用它的線程,直到參數 tid 指定的線程結束。 tid 指定的線程必須在當前進程中,且必須是非分離的。 status 接收指定線程終止時的返回狀態碼。

       不能有多個線程等待同一個線程終止,否則返回錯誤碼 ESRCH

       當 pthread_join() 返回時,終止線程占用的堆棧等資源已被回收。

       返回值:成功返回 0

       ESRCH  tid 指定的線程不是一個當前線程中合法且未分離的線程。

       EDEADLK  tid 指定的是當前線程。

       EINVAL  tid 非法。

 

分離一個線程

       pthread_detach(3T)

       將非分離線程設置為分離線程。

      

       int  pthread_detach( pthread_t tid );

       #include<pthread.h>

       pthread_t  tid;

       int  ret;

       ret = pthread_detach( tid );

      

       該函數通知線程庫,當線程終止以后, tid 所指線程的內存可以被立即收回。

       返回值:成功返回 0

       EINVAL  tid 所指線程不是一個合法線程。

       ESRCH  tid 指定的線程不是一個當前線程中合法且未分離的線程。

 

為線程數據創建一個鍵

       多線程的 c 語言程序具有三種數據:局部變量,全局變量,線程數據( TSD )

       TSD 類似于全局變量,但是線程私有的 。

       每個 TSD 都有個鍵同他相關聯。

       pthread_key_create(3T)

      

       int pthread_key_create( pthread_key_t *key, void (*destructor)(*void) );

       #include<pthread.h>

       pthread_key_t  key;

       int  ret;

       /*key create without destructor*/

       ret = pthread_key_create( &key, NULL );

      

       /*key create with destructor*/

       ret = pthread_key_destructor( &key, destructor );

      

       該函數成功時,份配的建放在 key 中,必須保證 key 指向的內存區有效 。 destructor 用來釋放不再需要的內存。

       返回值:成功返回 0

       EAGAIN  key 名字空間耗盡

       ENOMEM  沒有足夠的內存空間創建一個新的鍵。

 

刪除線程數據的鍵

       pthread_key_delete(3T)

       solaris 線程接口中沒有該函數

      

       int pthread_key_delete ( pthread_key_t key );

       #include<pthread.h>

       pthread_key_t  key;

       int  ret;

       ret = pthread_key_delete( key );

 

       在調用該函數之前,必須釋放和本線程相關聯的資源, pthread_key_delete() 不會引發鍵的解析函數。

       返回值:成功返回 0

       EINVAL  key 值非法

 

設置線程數據鍵

       pthread_setspecific(3T)

       設置和某個線程數據鍵綁定在一起的線程數據 (一般是指針)

       函數將指向專有數據的指針value 設置進  由key指示的結構體 中;


       int  pthread_setspecific ( pthread_key_t key, const void *value );

       #include<pthread.h>

       pthread_key_t  key;

       void *value;

       int  ret;

       ret = pthread_setspecific( key, value );

 

       返回值:成功返回 0

       ENOMEM  沒有足夠的虛擬內存

       EINVAL  key 值非法

       pthread_setspecific() 不釋放原來綁定在鍵上的內存,給一個鍵綁定新的指針時,必須釋放原指針指向的內存 ,否則會發生內存泄漏。

 

獲取線程數據鍵

       pthread_getspecific(3T)

       獲取綁定在線程數據鍵上的值,并在指定的位置存儲值

       返回存放在對應結構體中的專有指針;

       int  pthread_getspecific( pthread_key_t key, void**value )

       #include<pthread.h>

       pthread_key_t  key;

       void  *value;

       pthread_getspecific( key, &value );

 

       返回值:不返回錯誤碼

 

取線程標識符

       pthread_self(3T)

       取當前線程的標識符

      

       pthread_t  pthread_self( void );

       #include<pthread.h>

       pthread_t  tid;

       tid = pthread_self();

 

       返回值:當前線程標識符。

 

比較線程標識符

       pthread_equal(3T)

      

       int  pthread_equal( pthread_t tid1, pthread_t tid2 );

       #include<pthread.h>

       pthread_t  tid1,tid2

       int  ret;

       ret = pthread_equal( tid1, tid2 );

 

       返回值:如果 tid1 和 tid2 相同返回非零值,否則返回 0 。如果參數非法,返回值不可預知。

 

初始化線程

       pthread_once(3T)

       用來調用初始化函數,只有第一次調用有效。

       int  pthread_once( pthread_once_t *once_control, void(*init_routine)(void) );

       #include<pthread.h>

       pthread_once_t  once_control = PTHREAD_ONCE_INIT;

       int  ret;

       ret = pthread_once( &once_control, init_routine );

 

       once_control 界定了相應的初始化函數是否被調用過。

       返回值:成功返回 0

       EINVAL  某個參數為 NULL

 

出讓當前線程對處理器的控制權

       sched_yeild(3R)

       把當前線程的優先權讓給有相同或更高優先級的線程。

       int  sched_yeild( void );

       #include<pthread.h>

       int  ret;

       ret = sched_yeild();

 

       返回值:成功返回 0

       ENOSYS  當前版本不支持 sched_yield()

 

設置線程的優先級

       pthread_setschedparam(3T)

      

       int  pthread_setschedparam( pthread_t tid, int policy, const struct sched_param *param );

       #include<pthread.h>

       pthread_t  tid;

       int  ret;

       struct  sched_param  param;

       int  priority;

      

       /*sched_priority will be the priority of the thread*/

       sched_param,sched_priority = priority;

       /*only supported policy ,other will result in ENOTSUP*/

       policy = SCHED_OTHER;

       /*scheduling parameters of target thread*/

       ret = pthread_setschedparam( tid, policy, &param );

      

       返回值:成功返回 0

EINVAL  屬性值非法

ENOTSUP  屬性值在當前版本不支持

 

取線程的優先級

       pthread_getschedparam(3T)


      

       int  pthread_getschedparam( pthread_t tid, int policy, struct schedparam *param );

       #include<pthread.h>

       pthread_t  tid;

       sched_param  param;

       int  prioprity;

       int  policy;

       int  ret;

       /*scheduling parameters of target thread*/

       ret = pthread_getschedparam( tid, &policy, &param );

       /*sched_priority contains the priority of the thread*/

       priority = param.sched_priority;

 

       返回值:成功返回 0

       ESRCH  tid 不是一個現存的線程。

 

向線程發信號

       pthread_kill(3T)

      

       int  pthread_kill( pthread_t tid, int sig );

       #include<pthread.h>

       #include<signal.h>

       int  sig;

       pthread_t  tid;

       int  ret;

       ret = pthread_kill( tid, sig );

 

       tid 指定的線程必須和函數當前線程在同一個進程中。

       sig 為 0 時,進行錯誤檢查,不發送信號,往往被用來檢查 tid 的合法性。

       返回值:成功返回 0

       EINVAL  sig 不是合法信號量

       ESRCH  tid 不是當前進程中的線程

 

訪問當前線程的信號掩碼

       pthread_sigmask(3T)

      

       int  pthread_sigmask( int how, const sigset_t *new, sigset_t *old );

#include<pthread.h>

       #include<signal.h>

       int  ret;

       sigset_t  old, new;

       ret = pthread_sigmask( SIG_SETMASK, &new, &old );

       ret = pthread_sigmask( SIG_BLOCK, &new, &old );

ret = pthread_sigmask( SIG_UNBLOCK, &new, &old );

 

       how 表示對當前信號掩碼進行什么操作。

              SIG_SETMASK :在信號掩碼中加入 new 信號集, new 表示新增加的要屏蔽的信號。

              SIG_BLOCK :在信號掩碼中刪去 new 信號集, new 表示新增加的不需再屏蔽的信號。

              SIG_UNBLOCK :用 new 信號集替換信號掩碼, new 表示所有需要屏蔽的信號。

       當 new 為 NULL 時,無論 how 是什么,當前線程的信號掩碼都不會改變。

       舊的信號掩碼保存在 old 中。

       返回值:成功返回 0

       EINVAL  how 的值未定義

 

安全的復制

       pthread_atfork(3T)

      

       int  pthread_atfork( void(*prepare)(void), void(*parent)(void), void(*child)(void) );

 

終止線程

       pthread_exit(3T)

      

       void  pthread_exit(void *status);

       #include<pthread.h>

       int  status;

       pthread_exit( &status );

      

       終止當前線程,所有綁定在線程鍵上的內存將釋放。如果當前線程是未分離的,該線程的標識符和推出代碼( status )被保留,直到其它線程用 pthread_join() 等待當前線程的終止。如果當前線程是分離的, status 被忽略,線程標識符立即收回。

       返回值:若 status 不為 NULL ,線程的退出代碼被置為 status 指向的值。

 

       一個線程可以用一下方式終止自身運行。

       從線程的入口函數返回。

       調用 pthread_exit()

       用 POSIX 的 pthread_cancel()

 

       退出可以用以下方式:

       異步的

       由線程庫定義的一系列退出點

       有應用程序定義的一系列退出點

 

       退出點

       由程序通過 pthread_testcancel() 建立

       調用了 pthread_cond_wait() 或 pthread_cond_timedwait() 等待一個條件的線程

       調用了 pthread_join() 等待另一個線程結束的線程。

       被阻塞在 sigwait(2) 上的線程。

       一些標準的庫函數。

      

退出線程

       pthread_cancel(3T)

      

       int  pthread_cancel( pthread_t thread );

#include<pthread.h>

pthread_t  thread;

int  ret;

ret = pthread_cancel ( thread ) ;

 

返回值:成功返回 0

ESRCH  無指定的線程。

 

允許或禁止退出

       pthread_setcancelstate(3T)

       缺省是允許退出的。

       int  pthread_setcancelstate( int state, int *oldstate );

       #include<pthread.h>

       int  oldstate;

       int  ret;

       /*enable*/

       ret = pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, &oldstate );

       /*disabled*/

ret = pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, &oldstate );

 

返回值:成功返回 0

EINVAL  state 值非法

 

設置退出類型

       pthread_setcanceltype(3T)

       可以設置成延遲類型或異步類型。缺省是延遲類型。異步類型下,線程可以在執行中的任何時候被退出。

       int  pthread_setcanceltype( int type, int *oldtype );

       #include<pthread.h>

       int  oldtype;

       int  ret;

       /*deferred mode*/

       ret = pthread_setcanceltype( PTHREAD_CANCEL_DEFERRED, &oldtype );

       /*async mode*/

ret = pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype );

 

返回值:成功返回 0

EINVAL  state 值非法

 

創建退出點

       pthread_testcancel(3T)

       void  pthread_testcancel( void );

       #include<pthread.h>

       pthread_testcancel();

 

       只有當線程的退出狀態是允許退出,退出類型是延遲類型時,有效。

       沒有返回值。

 

將一個善后處理函數推入退出堆棧

       pthread_cleanup_push(3T)

       pthread_cleanup_pop(3T)

      

       void Pthread_cleanup_push( void(*routine)(void*), void *args );

       void pthread_cleanup_pop( int execute );

       #include<pthread.h>

       /*push the handler “routine” on cleanup stack*/

       pthread_cleanup_push( routine, arg );

       /*pop the “func” out of cleanup stack and execute “func”*/

       pthread_cleanup_pop( 1 );

       /*pop the “func” and don’t execute “func”*/

       pthread_cleanup_pop( 0 );

 

 

1.          線程屬性

只能在線程創建時制定屬性,不能在運行時改變它。

一旦屬性對象被配置好,它在進程范圍內都是有效的。

 

初始化屬性

pthread_attr_init(3T)

       初始化一個線程屬性對象,屬性值是缺省值,占用內存由線程庫分配。

       int  pthread_attr_init( pthread_attr_t *tattr );

       #include<pthread.h>

       pthread_attr_t  tattr;

       int  ret;

       /*initialize an attribute to the default value*/

       ret = pthread_attr_init( &tattr );

      

       屬性對象的缺省值:

       scope (線程域)        PTHREAD_SCOPE_PROCESS

       Detachstate (分離狀態)   PTHREAD_CREATE_JOINABLE

       Stackaddr (堆棧地址)   NULL

       Stacksize (堆棧大小)   1Mb

       priority (優先級)   父進程優先級

       Inheritsched (繼承調度優先級)   PTHREAD_INHERIT_SCHED

       schedpolicy (調度策略)   SCHED_OTHER

      

       返回值:成功返回 0

       ENOMEM  沒有足夠的內存初始化線程屬性對象

      

釋放屬性對象

pthread_attr_destroy(3T)

      

       int  pthread_attr_destroy( pthread_attr_t *tattr  );

       #include<pthread.h>

       pthread_attr_t  tattr;

       int  ret;

       ret = pthread_attr_destroy( &tattr );

      

       返回值:成功返回 0

       EINVAL  tattr 值非法

 

設置分離狀態

pthread_attr.setdetachstate(3T)

       創建線程時,如果指定這個線程為分離線程,一旦這個線程終止,他的線程標識符和其他相關的資源可以立即被使用。如果不需要等待某個線程終止,可以把它設定為分離。

int  pthread_attr_setdetachstate( pthread_attr_t *tattr, int detachstate );

#include<pthread.h>

       pthread_attr_t  tattr;

       int  ret;

       /*set the thread detach state*/

       ret = pthread_attr_setdetachstate( &tattr, PTHREAD_CREATE_DETACHED );

      

       返回值:成功返回 0

       EINVAL  tattr 的值或 detachstate 的值非法

 

取分離狀態

pthread_attr_getdetachstate(3T)

       int  pthread_attr_getdetachstate( const pthread_attr_t *tattr, int *detachstate );

       #include<pthread.h>

       pthread_attr_t  tattr;

       int  detachstate;

       int  ret;

       ret = pthread_attr_getdetachstate( &tattr,  &detachstate );

 

       返回值:成功返回 0

       EINVAL  tattr 的值或 detachstate 的值非法

 

本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/yuzhoudiwang/archive/2009/07/25/4379735.aspx

Feedback

# re: linux 線程函數大全  回復  更多評論   

2012-08-20 10:40 by qianliu
學習
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            亚洲天堂久久| 亚洲无玛一区| 亚洲人成网在线播放| 国产精品免费一区二区三区观看| 欧美呦呦网站| 欧美一区激情| 欧美亚洲日本一区| 午夜精彩国产免费不卡不顿大片| 一区二区三区高清| 在线性视频日韩欧美| 国产精品99久久久久久久女警| 欧美激情第五页| 欧美黑人多人双交| 欧美激情一区二区三级高清视频| 美女精品自拍一二三四| 男人的天堂亚洲在线| 亚洲黄网站在线观看| 亚洲精选在线观看| 欧美一区二区播放| 欧美成人一区二区| 国产精品久久久久久久久免费| 国产精品免费看| 亚洲日本乱码在线观看| 日韩一级黄色av| 欧美一区午夜精品| 欧美精品麻豆| 影音先锋亚洲精品| 午夜精品婷婷| 亚洲精品资源| 亚洲人成毛片在线播放女女| 亚洲区在线播放| 欧美v亚洲v综合ⅴ国产v| 久久精品国产久精国产思思| 亚洲日本成人| av成人免费在线观看| 国产精品亚洲аv天堂网| 另类av一区二区| 欧美色中文字幕| 久久综合成人精品亚洲另类欧美| 欧美成人一区二区三区| 午夜一区在线| 久久久久久成人| 99riav国产精品| 午夜精品视频在线观看一区二区| 国产午夜精品一区理论片飘花 | 麻豆成人综合网| 在线观看成人av| 免费不卡中文字幕视频| 欧美国产一区视频在线观看| 99在线观看免费视频精品观看| 中文一区二区| 亚洲激情电影中文字幕| 亚洲日本va午夜在线影院| 欧美成人精品在线| 一区二区三区黄色| 狠狠色丁香婷婷综合影院| 亚洲国产精品日韩| 国产婷婷色综合av蜜臀av| 久久精品国产清高在天天线| 欧美精品乱码久久久久久按摩| 亚洲电影在线| 国产亚洲网站| 99国产精品久久久| 亚洲欧洲免费视频| 欧美在线三级| 午夜精品在线| 欧美午夜欧美| 猛干欧美女孩| 国模吧视频一区| 亚洲欧美中文字幕| 亚洲欧美在线免费观看| 欧美视频日韩视频在线观看| 亚洲国产欧美一区二区三区久久 | 日韩午夜三级在线| 在线观看视频亚洲| 久久综合久久综合久久综合| 免费观看成人www动漫视频| 亚洲视频综合在线| 亚洲国产精品va在线看黑人| 国产精品视频最多的网站| 久久理论片午夜琪琪电影网| 一本色道久久综合亚洲精品婷婷 | 久久久久在线观看| 欧美fxxxxxx另类| 一本一本a久久| 国产亚洲一区二区三区| 久久亚洲图片| 亚洲人成在线观看一区二区| 国产精品嫩草影院av蜜臀| 免费亚洲电影| 欧美大色视频| 久久综合给合| 午夜国产欧美理论在线播放| 免费人成网站在线观看欧美高清| 亚洲精选在线| 亚洲综合日韩中文字幕v在线| 激情校园亚洲| 欧美电影在线免费观看网站| 麻豆成人综合网| 久久精品国产免费看久久精品| 国产日韩一区| 国产一区二区视频在线观看| 欧美视频中文字幕| 国产精品乱人伦一区二区| 国产精品xxxav免费视频| 国产精品v欧美精品v日韩| 国产精品美女主播| 狠狠色噜噜狠狠狠狠色吗综合| 激情伊人五月天久久综合| 美女主播精品视频一二三四| 亚洲一区日本| 久久深夜福利免费观看| 99re66热这里只有精品4| 国产精品v欧美精品v日韩精品| 久久久99精品免费观看不卡| 亚洲国产天堂久久综合| 久久一区二区三区四区| 亚洲精品欧美日韩专区| 欧美一区二区大片| 日韩视频二区| 99亚洲视频| 亚洲精选一区二区| 亚洲破处大片| 欧美国产日韩亚洲一区| 亚洲精品老司机| 国产伦理一区| 亚洲午夜精品17c| 亚洲免费成人av| 亚洲第一久久影院| 国产精品自在欧美一区| 欧美精品手机在线| 噜噜噜噜噜久久久久久91| 亚洲一区二区精品视频| 亚洲开发第一视频在线播放| 久久亚洲二区| 久久嫩草精品久久久久| 欧美一区二区三区四区视频 | 国产精品嫩草久久久久| 欧美国产丝袜视频| 久久激情久久| 欧美日韩国产综合视频在线观看中文| 国产日韩欧美综合在线| 黑人一区二区| 亚洲精品在线看| 亚洲精品一区在线| 欧美日韩国产精品一卡| 葵司免费一区二区三区四区五区| 99精品热视频| 香蕉久久夜色精品国产| 亚洲欧美日韩国产一区| 亚洲一区二区在| 亚洲图片欧美一区| 欧美日韩国产经典色站一区二区三区| 性欧美办公室18xxxxhd| 国产精品试看| 久久成人综合网| 久久一区二区三区av| 亚洲国产精品一区制服丝袜| 另类图片综合电影| 亚洲经典视频在线观看| 亚洲午夜电影在线观看| 国产精品久久久久毛片软件| 亚洲免费影院| 美女网站久久| 亚洲视频精选| 国内精品久久久久久久影视麻豆| 久久综合伊人77777麻豆| 亚洲精品中文字| 久久精品在线免费观看| 在线免费高清一区二区三区| 欧美韩日视频| 亚洲欧美日韩国产精品| 你懂的亚洲视频| 一本大道久久精品懂色aⅴ| 亚洲一区二区伦理| 一本一本a久久| 欧美高清视频在线播放| 国产毛片一区| 亚洲视频在线观看一区| 卡一卡二国产精品| 亚洲美女在线视频| 久久综合五月| 极品尤物久久久av免费看| 亚洲欧美日韩中文视频| 亚洲精品极品| 欧美色道久久88综合亚洲精品| 亚洲国产精品悠悠久久琪琪| 久久香蕉精品| 久久人人九九| 夜夜爽99久久国产综合精品女不卡| 久久九九久久九九| 亚洲欧美日本另类| 国产伦精品一区二区三区免费迷 | 国产精品伦子伦免费视频| 在线观看国产日韩| 亚洲免费激情| 中国女人久久久| 亚洲精品久久7777| 欧美福利在线观看| 国产精品扒开腿爽爽爽视频|