Linux下C多線程編程初學

來源:互聯網
上載者:User

抄襲的第一個Linux下C多線程並發的程式,要用到pthread.h標頭檔~這還不算完,記得編譯時間要用-lpthread來連結libpthread.a

原理很簡單,利用sleep(1)隔一秒列印一次,兩個線程分別列印"hello"和"world!\n"雖然寫的睡一秒,但是這兩個列印並不是均勻的交叉。

裡邊用到很多現成的線程的介面,pthread_create還有pthread_join(),還有pthread_t類型。除此,有一個sleep()

/*Hello,world--Single Thread*///記得-lpthread連結libpthread.a#include<stdio.h>#include<pthread.h>#define NUM 6int main(){void print_msg(void*);pthread_t t1, t2;pthread_create(&t1, NULL, print_msg, (void*)"hello,");pthread_create(&t2, NULL, print_msg, (void*)"world!\n");pthread_join(t1, NULL);pthread_join(t2, NULL);}void print_msg(void* m){char *cp = (char*)m;//有個轉換過程int i;for(i = 0 ; i < NUM ; i++){//隔一秒列印printf("%s", m);fflush(stdout);//清空sleep(1);//睡眠一秒}}/* * 函數原型: * int pthread_create(pthread_t *thread,pthread_attr_t *attr,void *(*func)(void*),void *arg); * 參數: thread 指向pthread_t類型變數的指標 * attr 指向pthread_attr_t類型變數的指標,或者為NULL * func 指向新線程所運行函數的指標 * arg 傳遞給func的參數 * 傳回值 0 成功返回 * errcode 錯誤 * 我們可以使用函數pthread_join等待某進程結束。 * 函數原型:int pthread_join(pthread_t thread,void ** retval); * 參數: thread 所等待的進程 * retval 指向某儲存線程傳回值的變數 * 傳回值: 0 成功返回 * errorcode 錯誤 * 以上兩個函數都包含在標頭檔pthread.h中。 */

來看第二個例子

/*thread_example.c :  c multiple thread programming in linux  *  */#include <pthread.h>#include <stdio.h>#include <sys/time.h>#include <string.h>#define MAX 10pthread_t thread[2];//建立一個線程的數組。。。pthread_mutex_t mut;//翻譯角度講,這是一個訊號量int number=0, i;void *thread1(){        printf ("thread1 : I'm thread 1\n");        for (i = 0; i < MAX; i++)        {                printf("thread1 : number = %d\n",number);                pthread_mutex_lock(&mut);                        number++;                pthread_mutex_unlock(&mut);                sleep(2);        }        printf("thread1 :主函數在等我完成任務嗎?\n");        pthread_exit(NULL);}void *thread2(){        printf("thread2 : I'm thread 2\n");        for (i = 0; i < MAX; i++)        {                printf("thread2 : number = %d\n",number);                pthread_mutex_lock(&mut);                        number++;                pthread_mutex_unlock(&mut);                sleep(3);        }        printf("thread2 :主函數在等我完成任務嗎?\n");        pthread_exit(NULL);}void thread_create(void){        int temp;        memset(&thread, 0, sizeof(thread));          //comment1        /*建立線程*/        if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0)       //comment2                printf("線程1建立失敗!\n");        else                printf("線程1被建立\n");        if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0)  //comment3                printf("線程2建立失敗");        else                printf("線程2被建立\n");}void thread_wait(void){        /*等待線程結束*/        if(thread[0] !=0) {                   //comment4                pthread_join(thread[0],NULL);                printf("線程1已經結束\n");        }        if(thread[1] !=0) {                //comment5                pthread_join(thread[1],NULL);                printf("線程2已經結束\n");        }}int main(){        /*用預設屬性初始化互斥鎖*/        pthread_mutex_init(&mut,NULL);        printf("我是主函數哦,我正在建立線程,呵呵\n");        thread_create();        printf("我是主函數哦,我正在等待線程完成任務阿,呵呵\n");        thread_wait();        return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.