最近學習c語言和linux,記錄一下linux中線程的簡單使用:
linux執行緒一般用pthread庫建立。pthread是 posix thread的簡稱。
在linux的/lib目錄下,可以找到名為libpthread-x.x.so(x.x是版本號)的庫。下面模擬兩個執行緒:
/** threaddemo.c
** created on: 2010-9-29
* author: cmzx3444
*/#include
#include
#include
#include
#define max 10
pthread_t thread[2];
pthread_mutex_t mut;
int number = 0, i;
void *thread1()
printf("thread1 :主函式在等我完成任務嗎?\n");
pthread_exit(null);
}void *thread2()
printf("thread2 :主函式在等我完成任務嗎?\n");
pthread_exit(null);
}void thread_create(void)
void thread_wait(void)
if (thread[1] != 0)
}int main()
然後編譯:
gcc -o test thread.c –lpthread
編譯必須加上 -lpthread引數。意思是鏈結 pthread庫。
linux下C語言多執行緒程式設計
include include include include define max 10pthread t thread 2 pthread mutex t mut int number 0 i void thread1 printf thread1 主函式在等我完成任務嗎?n pthread e...
多執行緒程式設計 c語言linux下
適用與linux系統 1.了解基本概念 程序 是計算機所執行的乙個任務的描述,是面向作業系統的最小單位,作業系統能執行很多程序 執行自己寫的乙份 程式,就是讓作業系統執行乙個自己程式的程序 作業系統會根據程式分配定量的資源 執行緒 面想程式 程序 的,把乙個程式分成多個執行緒可以實現並髮式,多工執行...
linux下C語言多執行緒程式設計例項
我們通過建立兩個執行緒來實現對乙個數的遞加。或許這個例項沒有實際運用的價值,但是稍微改動一下,我們就可以用到其他地方去拉。下面是我們的 thread example.c c multiple thread programming in linux author falcon e mail tunzh...