参考 极客时间:趣谈Linux操作系统
多线程执行
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#define NUM_OF_TASKS 5
void *downloadfile(void *filename){
printf("I am downloading thr file %s!\n)",(char*)filename);
sleep(10);
long downloadtime = rand()%100;
printf("I am finish downloading the file within %d minutes!\n",downloadtime);
pthread_exit((void*)downloadtime);
}
int main(int argc,char* argv[]){
char files[NUM_OF_TASKS][20]={"file1.av1","file2.rmvb","file3.mp4","file4.wmv","file5.flv"};
pthread_t threads[NUM_OF_TASKS];
int rc;
int t;
int downloadtime;
pthread_attr_t thread_attr;
pthread_attr_init(&thread_attr);
pthread_attr_setdetachstate(&thread_attr,PTHREAD_CREATE_JOINABLE);
for(t=0;t<NUM_OF_TASKS;t++){
printf("ctreating thread %d,please help me to download %s\n",t,files[t]);
rc=pthread_create(&threads[t],&thread_attr,downloadfile,(void*)files[t]);
if(rc){
printf("ERROR;return code from pthread_create() is %d\n",rc);
exit(-1);
}
}
pthread_attr_destory(&thread_attr);
for(t=0;t<NUM_OF_TASKS;t++){
pthread_join(threads[t],(void**)&downloadtime);
printf("Thread %d downloads the file %s in %d minutes.\n",t,files[t],downloadtime);
}
pthread_exit(NULL);
}
互斥锁多线程执行
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#define NUM_OF_TASKS 5
int money_of_tom = 100;
int money_of_jerry = 100;
pthread_mutex_t g_money_lock;
void *transfer(void* notused){
pthread_t tid=pthread_self();
printf("Thread %u is transfering money!\n",(unsigned int)tid);
pthread_mutex_lock(&g_money_lock);
sleep(rand()%10);
money_of_tom+=10;
sleep(rand()%10);
money_of_jerry-=10;
pthread_mutex_unlock(&g_money_lock);
printf("Thread %u finish transfering money!\n",(unsigned int)tid);
pthread_exit((void*)0);
}
int main(int argc,char* argv[]){
pthread_t threads[NUM_OF_TASKS];
int rc;
int t;
pthread_mutex_init(&g_money_lock,NULL);
for(t=0;t<NUM_OF_TASKS;t++){
rc=pthread_create(&threads[t],NULL,transfer,NULL);
if(rc){
printf("ERROR;return code from pthread_create() is %d\n",rc);
exit(-1);
}
}
for(t=0;t<100;t++){
pthread_mutex_lock(&g_money_lock);
printf("money_of_tom+money_of_jerry = %d\n",money_of_tom+money_of_jerry);
pthread_mutex_unlock(&g_money_lock);
pthread_exit(NULL);
}
}
加入环境变量和互斥锁的多线程执行
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#define NUM_OF_TASKS 3
#define MAX_TASK_QUEUE 11
char tasklist[MAX_TASK_QUEUE]="ABCDEFGHIJ";
int head=0;
int tail=0;
int quit=0;
pthread_mutex_t g_task_lock;
pthread_cond_t g_task_cv;
void* coder(void *notused){
pthread_t tid=pthread_self();
while(!quit){
pthread_mutex_lock(&g_task_lock);
while(head==tail){
if(quit){
pthread_mutex_unlock(&g_task_lock);
pthread_exit((void*)0);
}
printf("No task now ! Thread %u is grabing the task!\n",(unsigned int)tid);
}
char task=tasklist[head++];
pthread_mutex_unlock(&g_task_lock);
printf("Thread %u has a task %c now!\n",(unsigned int)tid,task);
printf("Thread %u finish the task %c!\n",(unsigned int)tid,task);
}
pthread_exit((void*)0);
}
int main(int argc,char* argv[]){
pthread_t threads[NUM_OF_TASKS];
int rc;
int t;
pthread_mutex_init(&g_task_lock,NULL);
pthread_cond_init(&g_task_cv,NULL);
for(t=0;t<NUM_OF_TASKS;t++){
rc=pthread_create(&threads[t],NULL,coder,NULL);
if(rc){
printf("ERROR;return code from pthread_create() is %d \n",rc);
exit(-1);
}
}
for(t=1;t<=4;t++){
pthread_mutex_lock(&g_task_lock);
tail+=t;
printf("I am boss,I assigned %d tasks,I notify all coders!\n",t);
pthread_cond_broadcast(&g_task_cv);
pthread_mutex_unlock(&g_task_lock);
}
pthread_mutex_lock(&g_task_lock);
quit=1;
pthread_cond_broadcast(&g_task_cv);
pthread_mutex_unlock(&g_task_lock);
pthread_mutex_destroy(&g_task_lock);
pthread_cond_destroy(&g_task_cv);
pthread_exit(NULL);
}