2011年12月1日星期四

linux chapter11

POSIX线程
事实上,在linux上,线程的开销并不比进程少多少,所以不要迷信线程.
新老线程是共享非局部变量的,而父子进程则不.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<string.h>
char message[]="Hello wORLD";
void *thread_function(void *arg){
printf("thread_function is running.argument is %s\n",(char *)arg);
sleep(3);
strcpy(message,"BYE");
pthread_exit("Thank you for cpu time");
}

int main(){
int res;
pthread_t a_thread;
void *thread_result;
res=pthread_create(&a_thread,NULL,thread_function,(void *)message);
if(res!=0){exit(1);}
printf("waiting for thread to finish\n");
res=pthread_join(a_thread,&thread_result);
if(res!=0)exit(2);
printf("Thread join ,it return %s\n",(char *)thread_result);
printf("message is now %s\n",message);
exit(0);
}

用信号量进行同步
(信号量可以同步线程(POSIX REALTIME EXTENSION,也可以同步进程SYSTEM V)
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<string.h>
#include<semaphore.h>
#define WORK_SIZE 1024
char work_area[WORK_SIZE];
sem_t bin_sem;
void *thread_function(void *arg){
sem_wait(&bin_sem);
while(strncmp("end",work_area,3)!=0){
printf("you input %d characters\n",strlen(work_area)-1);
sem_wait(&bin_sem);
}
pthread_exit(0);
}

int main(){
int res;
pthread_t a_thread;
void *thread_result;
res=sem_init(&bin_sem,0,0);
if(res!=0){
perror("semaphore initialization failed\n");
exit(1);
}

res=pthread_create(&a_thread,NULL,thread_function,0);
if(res!=0){exit(1);}
printf("input some text.enter 'end' to finish\n");
while(strncmp("end",work_area,3)!=0){
fgets(work_area,WORK_SIZE,stdin);
sem_post(&bin_sem);
}
printf("\n waiting for thread to finish\n");
res=pthread_join(a_thread,&thread_result);
if(res!=0)exit(2);
printf("Thread join\n");
sem_destroy(&bin_sem);
exit(0);
}

用互斥量进行同步
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<string.h>
#include<semaphore.h>
#define WORK_SIZE 1024
char work_area[WORK_SIZE];
int time_to_exit=0;
void *thread_function(void *arg);
pthread_mutex_t work_mutex;
int main(){
int res;
pthread_t a_thread;
void *thread_result;
res=pthread_mutex_init(&work_mutex,0);
res=pthread_create(&a_thread,0,thread_function,0);
pthread_mutex_lock(&work_mutex);
printf("input some text,enter end to finish\n");
while(!time_to_exit){
fgets(work_area,WORK_SIZE,stdin);
pthread_mutex_unlock(&work_mutex);
while(1){
pthread_mutex_lock(&work_mutex);
if(work_area[0]!='\0'){
pthread_mutex_unlock(&work_mutex);
sleep(1);
}
else break;
}
}
pthread_mutex_unlock(&work_mutex);
printf("\nwaiting for thread to finish\n");
res=pthread_join(a_thread,&thread_result);
printf("thread joined\n");
pthread_mutex_destroy(&work_mutex);
exit(0);
}
void *thread_function(void *arg){
sleep(1);
pthread_mutex_lock(&work_mutex);
while(strncmp("end",work_area,3)!=0){
printf("you input %d characters\n",strlen(work_area)-1);
work_area[0]='\0';
pthread_mutex_unlock(&work_mutex);
sleep(1);
pthread_mutex_lock(&work_mutex);
while(work_area[0]=='\0'){
pthread_mutex_unlock(&work_mutex);
sleep(1);
pthread_mutex_lock(&work_mutex);
}
}
time_to_exit=1;
work_area[0]='\0';
pthread_mutex_unlock(&work_mutex);
pthread_exit(0);
}

脱离型线程:不需规并,自己结束
取消线程(不必等,暴力消灭)
多线程示例:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<string.h>
#include<semaphore.h>
#define NUM_THREAD 6
void *thread_function(void *arg);
int main(){
int res;
pthread_t a_thread[NUM_THREAD];
void *thread_result;
int lots_of_thread;
for(lots_of_thread=0;lots_of_thread<NUM_THREAD;lots_of_thread++){
res=pthread_create(&(a_thread[lots_of_thread]),0,thread_function,(void *)&lots_of_thread);
sleep(1);
}
printf("waiting for threads to finish\n");
for(lots_of_thread=NUM_THREAD-1;lots_of_thread>-1;lots_of_thread--){
res=pthread_join(a_thread[lots_of_thread],&thread_result);
printf("picking up a thread\n");
}
printf("all done\n");
exit(0);
}
void *thread_function(void *arg){
int my_number=*(int *)arg;
int rand_num;
printf("thread_function is running,argument is %d\n",my_number);
rand_num=1+(int)(9.0*rand()/(RAND_MAX+1.0));
sleep(rand_num);
printf("BYE from %d\n",my_number);
pthread_exit(0);
}

没有评论:

发表评论