c文件:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void printfids(const char *s){
pid_t pid;
pthread_t tid;
pid=getpid();
tid=pthread_self();
printf("%s.pid=%u,tid= %u\n",s,pid,tid);
}
void * fn1(void * args){
printfids("child thread");
return ((void*)0);
}
void main(){
pthread_t tid;
int err;
err=pthread_create(&tid,NULL,fn1,NULL);
if(err!=0){
printf("fail to create thread .%s\n",strerror(err));
}
printfids("main thread");
sleep(2);
exit(0);
}
makefile文件:
thread_create : threadcreate.o
cc -o thread_create threadcreate.o -lpthread
threadcreate.o:threadcreate.c
cc -c threadcreate.c
这里要注意的是,由于pthread 库不是 Linux 系统默认的库,在链接的时候,需要使用静态库 libpthread.a,这就是在makefile文件中,cc -o后面带了lpthread的原因。
//互斥量同步的简单例子
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
struct MemUse{
int f_count;
pthread_mutex_t f_lock;
};
struct MemUse *fp;
int val=0;
//alloc
struct MemUse * allocMemUse(){
struct MemUse* fp;
int err;
fp=malloc(sizeof(struct MemUse));
if(fp!=NULL){
fp->f_count=0;
err=pthread_mutex_init(&fp->f_lock,NULL);
if(err!=0){
printf("fail to init mutex!%s\n",strerror(err));
free(fp);
return NULL;
}
}
return fp;
}
//hold
void holdMemUse(struct MemUse* fp){
pthread_mutex_lock(&fp->f_lock);
fp->f_count++;
pthread_mutex_unlock(&fp->f_lock);
}
//release
void releMemUse(struct MemUse* fp){
pthread_mutex_lock(&fp->f_lock);
fp->f_count--;
pthread_mutex_unlock(&fp->f_lock);
}
//print
void printInfo(struct MemUse* fp){
printf("fp->f_count=%d\n",fp->f_count);
}
//print thread
void printThread(const char* str){
pid_t pid;
pthread_t tid;
tid=pthread_self();
pid=getpid();
printf("in thread,pid=%u,tid=%u,%s\n",(int)pid,(int)tid,str);
}
//addfun
void * addVal(void* val){
printThread("child thread try to add val-------");
holdMemUse(fp);
int *loc=(int*)val;
// sleep(2);
*loc=*loc+1;
releMemUse(fp);
printf("finish add .val=%d\n",*loc);
return ((void*)*loc);
}
void main(){
printThread("main thread");
fp=allocMemUse();
if(fp!=NULL){
holdMemUse(fp);
printInfo(fp);
releMemUse(fp);
printInfo(fp);
}
int val=0;
int i=0;
int len=10;
pthread_t tid[len];
printf("initial val is %d\n",val);
for(i=0;i<len;i++){
pthread_create(&tid[i],NULL,addVal,&val);
sleep(1);
printf("mid stage,val=%d\n",val);
}
sleep(2);
printf("before join ,val=%d\n",val);
void *ret;
for(i=0;i<len;i++){
printf("wwwwwwwwwwwwwww join thread:%u\n",(int)tid[i]);
pthread_join(tid[i],&ret);
printf("wwwwwwwwwwwwwww finish join thread:%u,exit code is :%d\n",(int )tid[i],(int*)ret);
}
printf("after join , value is : val=%d\n"+val);
pthread_mutex_destroy(&fp->f_lock);
}
一个很奇怪的问题是,最后打印的val的值不是预想中的10,而是随机值,这个是什么原因?并且,“after join"这些也没有输出,里面发生什么了?。。。
具体的输出:
in thread,pid=7117,tid=3078182592,main thread
fp->f_count=1
fp->f_count=0
initial val is 0
in thread,pid=7117,tid=3078179696,child thread try to add val-------
finish add .val=1
mid stage,val=1
in thread,pid=7117,tid=3067689840,child thread try to add val-------
finish add .val=2
mid stage,val=2
in thread,pid=7117,tid=3057199984,child thread try to add val-------
finish add .val=3
mid stage,val=3
in thread,pid=7117,tid=3046710128,child thread try to add val-------
finish add .val=4
mid stage,val=4
in thread,pid=7117,tid=3036220272,child thread try to add val-------
finish add .val=5
mid stage,val=5
in thread,pid=7117,tid=3025730416,child thread try to add val-------
finish add .val=6
mid stage,val=6
in thread,pid=7117,tid=3015240560,child thread try to add val-------
finish add .val=7
mid stage,val=7
in thread,pid=7117,tid=3004750704,child thread try to add val-------
finish add .val=8
mid stage,val=8
in thread,pid=7117,tid=2994260848,child thread try to add val-------
finish add .val=9
mid stage,val=9
in thread,pid=7117,tid=2983770992,child thread try to add val-------
finish add .val=10
mid stage,val=10
before join ,val=10
wwwwwwwwwwwwwww join thread:3078179696
wwwwwwwwwwwwwww finish join thread:3078179696,exit code is :1
wwwwwwwwwwwwwww join thread:3067689840
wwwwwwwwwwwwwww finish join thread:3067689840,exit code is :2
wwwwwwwwwwwwwww join thread:3057199984
wwwwwwwwwwwwwww finish join thread:3057199984,exit code is :3
wwwwwwwwwwwwwww join thread:3046710128
wwwwwwwwwwwwwww finish join thread:3046710128,exit code is :4
wwwwwwwwwwwwwww join thread:3036220272
wwwwwwwwwwwwwww finish join thread:3036220272,exit code is :5
wwwwwwwwwwwwwww join thread:3025730416
wwwwwwwwwwwwwww finish join thread:3025730416,exit code is :6
wwwwwwwwwwwwwww join thread:3015240560
wwwwwwwwwwwwwww finish join thread:3015240560,exit code is :7
wwwwwwwwwwwwwww join thread:3004750704
wwwwwwwwwwwwwww finish join thread:3004750704,exit code is :8
wwwwwwwwwwwwwww join thread:2994260848
wwwwwwwwwwwwwww finish join thread:2994260848,exit code is :9
wwwwwwwwwwwwwww join thread:2983770992
wwwwwwwwwwwwwww finish join thread:2983770992,exit code is :10
, value is : val=-1311196304
红色的输出是不齐全,且是错的的值。