系統(tǒng)編程-------線程編程----線程創(chuàng)建和調(diào)度

線程的創(chuàng)建和調(diào)度

1退客、線程的創(chuàng)建

pthread_create創(chuàng)建線程

#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);

參數(shù):

  • pthread_t *thread 指定創(chuàng)建線程id
  • const pthread_attr_t *attr;NULL
  • void (start_routine) (void *): 線程函數(shù)
  • void *arg:線程參數(shù)

返回值:成功,返回0,;出錯(cuò),返回錯(cuò)誤值;

2、等待線程的返回值

pthread_join();等待線程返回值

#include <pthread.h>
int pthread_join(pthread_t thread, void **retval);

參數(shù):

  • pthread_t thread :線程ID號(hào)
  • void **retval:線程返回結(jié)果

返回值:成功卒暂,返回0;失敗娄帖,返回錯(cuò)誤號(hào)也祠;

3、獲取線程自身線程ID

pthread_self();

#include <pthread.h>
pthread_t pthread_self(void);

參數(shù):無(wú)

返回值:自身線程ID近速;

4诈嘿、判斷線程是否為同一個(gè)線程

pthread_equal()

#include <pthread.h>
int pthread_equal(pthread_t t1, pthread_t t2);

參數(shù):

  • 線程1的ID
  • 線程2的ID

返回值:相同,返回非0削葱;不相同奖亚,返回0;

5析砸、取消指定線程

pthread_cancel();

#include <pthread.h>
int pthread_cancel(pthread_t thread);

參數(shù):線程ID號(hào)

返回值:成功昔字,返回0;失敗,返回非0作郭;

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

// 線程函數(shù):用于提供線程的執(zhí)行指令代碼
void *thread_func1(void *arg);

void *thread_func2(void *arg);

int main(int argc, char *argv[])
{
    pthread_t thread_id1;
    pthread_t thread_id2;

    // 創(chuàng)建線程并指定線程的執(zhí)行函數(shù)
    pthread_create(&thread_id1, NULL, thread_func1, NULL);
    pthread_create(&thread_id2, NULL, thread_func2, NULL);

    // 等待指定線程的退出
    // 注意:主線程退出陨囊,則進(jìn)程退出,該進(jìn)程下的相關(guān)線程全部退出
    pthread_join(thread_id1, NULL);
    pthread_join(thread_id2, NULL);

    return 0;
}

void *thread_func1(void *arg)
{
    while(1)
        printf("A");

    pthread_exit(NULL);
}

void *thread_func2(void *arg)
{
    while(1)
        printf("B");
    
    pthread_exit(NULL);
}
//使用雙線程求解1~100的和
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>

// 定義傳遞個(gè)線程的參數(shù)結(jié)構(gòu)類型
struct thread_arg
{
    int thread_no;      // 線程編號(hào)(自定義的)
    int lower;          // 區(qū)間下限
    int upper;          // 區(qū)間上限
    // int sum;         // 傳出結(jié)果方法2夹攒,可以用作接收線程計(jì)算完畢后返回的整數(shù)結(jié)果
};

// 定義用于傳遞線程參數(shù)使用的全部變量蜘醋,每一個(gè)線程一個(gè)
struct thread_arg argument1, argument2;

// 線程函數(shù):用于提供線程執(zhí)行的指令代碼
void *add_thread_func(void *arg);

int main(int argc, char *argv[])
{
    pthread_t thread_id1;
    pthread_t thread_id2;
    // 定義指向線程返回結(jié)果空間的地址
    int *psum1 = NULL;
    int *psum2 = NULL;
    
    // 創(chuàng)建線程1
    argument1.thread_no = 1;
    argument1.lower = 1;
    argument1.upper = 50;
    pthread_create(&thread_id1, NULL, add_thread_func, &argument1);
    // 創(chuàng)建線程2
    argument2.thread_no = 2;
    argument2.lower = 51;
    argument2.upper = 100;
    pthread_create(&thread_id2, NULL, add_thread_func, &argument2);
    
    // 等待線程的退出,并接收線程返回值
    pthread_join(thread_id1, &psum1);
    pthread_join(thread_id2, &psum2);
    
    printf("sum = %d\n", *psum1+*psum2);

    // 釋放從線程中得到的結(jié)果空間
    free(psum1);
    free(psum2);

    return 0;
}

// 線程函數(shù)
void *add_thread_func(void *arg)
{
    struct thread_arg *p_arg = (struct thread_arg *)arg;
    int i = 0;
    int sum = 0;
    int *p_res = NULL;
    
    // 累和
    for(i = p_arg->lower; i < p_arg->upper+1; i++)
        sum += i;
     
    //printf("in thread no %d : sum = %d\n", p_arg->thread_no, sum);
    // 開辟空間存放結(jié)果
    p_res = (int *)malloc(sizeof(int));
    *p_res = sum;
    // 返回結(jié)果方法2咏尝,使用線程參數(shù)使用的全局變量返回
    //p_arg->sum = sum;
    
    // 線程返回結(jié)果并退出
    pthread_exit(p_res);   
}  


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末压语,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子编检,更是在濱河造成了極大的恐慌胎食,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,509評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件允懂,死亡現(xiàn)場(chǎng)離奇詭異斥季,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)累驮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)舵揭,“玉大人谤专,你說我怎么就攤上這事∥缟” “怎么了置侍?”我有些...
    開封第一講書人閱讀 163,875評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)拦焚。 經(jīng)常有香客問我蜡坊,道長(zhǎng),這世上最難降的妖魔是什么赎败? 我笑而不...
    開封第一講書人閱讀 58,441評(píng)論 1 293
  • 正文 為了忘掉前任秕衙,我火速辦了婚禮,結(jié)果婚禮上僵刮,老公的妹妹穿的比我還像新娘据忘。我一直安慰自己,他們只是感情好搞糕,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評(píng)論 6 392
  • 文/花漫 我一把揭開白布勇吊。 她就那樣靜靜地躺著,像睡著了一般窍仰。 火紅的嫁衣襯著肌膚如雪汉规。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評(píng)論 1 302
  • 那天驹吮,我揣著相機(jī)與錄音针史,去河邊找鬼晶伦。 笑死,一個(gè)胖子當(dāng)著我的面吹牛悟民,可吹牛的內(nèi)容都是我干的坝辫。 我是一名探鬼主播,決...
    沈念sama閱讀 40,190評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼射亏,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼近忙!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起智润,我...
    開封第一講書人閱讀 39,062評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤及舍,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后窟绷,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體锯玛,經(jīng)...
    沈念sama閱讀 45,500評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評(píng)論 3 335
  • 正文 我和宋清朗相戀三年兼蜈,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了攘残。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,834評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡为狸,死狀恐怖歼郭,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情辐棒,我是刑警寧澤病曾,帶...
    沈念sama閱讀 35,559評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站漾根,受9級(jí)特大地震影響泰涂,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜辐怕,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評(píng)論 3 328
  • 文/蒙蒙 一逼蒙、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧秘蛇,春花似錦其做、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至艘策,卻和暖如春蹈胡,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工罚渐, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留却汉,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,958評(píng)論 2 370
  • 正文 我出身青樓荷并,卻偏偏與公主長(zhǎng)得像合砂,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子源织,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容