2016英特爾在線筆試題(C語言)

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

void* print1(void* data){
    printf("1 ");
}

void* print2(void* data){
    printf("2 ");
}

void* print3(void* data){
    printf("3 ");
}

int main(void){
    pthread_t t,t1,t2;

    pthread_create(&t,0,print1,NULL);
    pthread_create(&t1,0,print2,NULL);
    pthread_create(&t2,0,print3,NULL);

    pthread_join(t,NULL);
    pthread_join(t1,NULL);
    pthread_join(t2,NULL);
    printf("\n");
}

上面程序的輸出是什麼(  )

A.   1  2 3
B.    1 3 2
C.    3 2 1
D.   不確定

答案D 解析:

pthread_create 是類Unix操作系統(Unix、Linux、Mac OS X等)的創建線程的函數。

頭文件:1
#include<pthread.h>
1
2
int pthread_create(pthread_t *tidp,const pthread_attr_t *attr, (void*)(*start_rtn)(void*),void *arg);
pthread_join 用來等待一個線程的結束。
頭文件 : #include <pthread.h>
函數定義: int pthread_join(pthread_t thread, void **retval);
描述 :pthread_join()函數,以阻塞的方式等待thread指定的線程結束。當函數返回時,被等待線程的資源被收回。如果線程已經結束,那麼該函數會立即返回。並且thread指定的線程必須是joinable的。
參數 :thread: 線程標識符,即線程ID,標識唯一線程。retval: 用戶定義的指針,用來存儲被等待線程的返回值。
返回值 : 0代表成功。 失敗,返回的則是錯誤號。
編譯命令: gcc test.c -lpthread -o test


發佈了23 篇原創文章 · 獲贊 20 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章