一下代码主要实现了linux下线程创建的基本方法,这些都是使用默认属性的。以后有机会再探讨自定义属性的情况。主要是为了练习三种基本的线程同步方法:互斥、读写锁以及条件变量。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h> int g_count = ;
pthread_mutex_t mutex_lock;
pthread_rwlock_t rw_lock;
pthread_cond_t con_val = PTHREAD_COND_INITIALIZER; typedef enum
{
MutexLock = ,
RWLock,
CondLock
}LockType; typedef struct
{
int val;
LockType type;
}ThreadData; void PrintThreadId()
{
pid_t pid;
pthread_t tid; pid = getpid();
tid = pthread_self(); printf("[%s] process id : %d thread id : 0x%lx\n",__func__,pid,tid);
} void CleanUpFun(void *arg)
{ printf("[%s] clean up : %s \n",__func__,(char *)arg);
} void AddCount(int tid,LockType type)
{
if(type == MutexLock)
{
printf("[%s] thread %d MutexLock the count is : %d\n",__func__,tid,g_count);
pthread_mutex_lock(&mutex_lock);
while(g_count < )
{
usleep();
printf("%d-%d \t",tid,g_count);
g_count++;
}
printf("\n");
pthread_mutex_unlock(&mutex_lock);
} if(type == RWLock)
{
pthread_rwlock_wrlock(&rw_lock);
printf("[%s] thread %d RWLock the count is : %d\n",__func__,tid,g_count);
while(g_count < )
{
usleep();
printf("%d-%d \t",tid,g_count);
g_count++;
}
printf("\n");
pthread_rwlock_unlock(&rw_lock); } if(type == CondLock)
{
printf("[%s] thread %d CondLock the count is : %d\n",__func__,tid,g_count);
pthread_mutex_lock(&mutex_lock); g_count = ;
printf("[%s] thread %d CondLock the count is : %d\n",__func__,tid,g_count); pthread_mutex_unlock(&mutex_lock); pthread_cond_signal(&con_val);
} } void DelCount(int tid,LockType type)
{
usleep(); if(type == MutexLock)
{
pthread_mutex_lock(&mutex_lock); printf("[%s] thread %d MutexLock the count is : %d\n",__func__,tid,g_count);
while(g_count > )
{
usleep();
printf("%d-%d \t",tid,g_count);
g_count--;
}
printf("\n");
pthread_mutex_unlock(&mutex_lock);
} if(type == RWLock)
{
pthread_rwlock_wrlock(&rw_lock); printf("[%s] thread %d RWLock the count is : %d\n",__func__,tid,g_count);
while(g_count > )
{
usleep();
printf("%d-%d \t",tid,g_count);
g_count--;
}
printf("\n");
pthread_rwlock_unlock(&rw_lock); } if(type == CondLock)
{
pthread_mutex_lock(&mutex_lock); // printf("[%s] thread %d CondLock the count is : %d\n",__func__,tid,g_count); //while(1)
{
pthread_cond_wait(&con_val,&mutex_lock); printf("[%s] thread %d CondLock the count is : %d\n",__func__,tid,g_count);
} pthread_mutex_unlock(&mutex_lock);
} } void PrintCount(int tid,LockType type)
{
if(type == RWLock)
{
pthread_rwlock_rdlock(&rw_lock); printf("[%s] thread %d RWLock the count is : %d\n",__func__,tid,g_count); pthread_rwlock_unlock(&rw_lock);
}
else
{ } } void ChangCount(int tid,LockType type)
{ if((tid == ) || (tid == ))
{
AddCount(tid,type);
}
else if((tid == ))
{
DelCount(tid,type);
}
else if(tid == )
{
PrintCount(tid,type);
} } void * ThreadFun(ThreadData *t)
{
printf("\n----------------------------------------------------------\n"); int val = ;
LockType type = ;
val = t->val;
type = t->type; printf("[%s] this is thread %d\n",__func__,val);
PrintThreadId(); char buf[];
sprintf(buf,"thread %d first handler ",val);
pthread_cleanup_push(CleanUpFun,buf);/*push and pop must be coupled*/ int len = strlen(buf);
sprintf(buf+len+,"thread %d second handler ",val);/*Notice !!! */
pthread_cleanup_push(CleanUpFun,buf+len+);/*the buf must start from different address , to the cleanupfunc ,the poniter is the same !!!*/ ChangCount(val,type); if(val == )
{
printf("----------------------------------------------------------\n");
return ((void *)val);/*clean up func won't run*/
}
else
{
printf("----------------------------------------------------------\n");
pthread_exit((void *)val);/*clean up func won run*/
}
pthread_cleanup_pop();
pthread_cleanup_pop(); return ((void *)val);
} void JoinThread(pthread_t tid)
{
void * ret; int err; err = pthread_join(tid,&ret); if(err != )
{
printf("error to join thread %lu\n",tid);
} printf("\n[%s] catch thread 0x%lx , the return val is %d\n",__func__,tid,(int)ret);
} void CreateThread(LockType type)
{
int err; ThreadData t1;
ThreadData t2;
ThreadData t3; t1.val = ;
t2.val = ;
t3.val = ; pthread_t tid_1;
pthread_t tid_2;
pthread_t tid_3; if(type == MutexLock)
{
/*Mutex lock*/
t1.type = MutexLock;
t2.type = MutexLock;
t3.type = MutexLock; err = pthread_create(&tid_1,NULL,(void *)ThreadFun,(void *)&t1);
if(err != )
{
printf("error to create thread !\n");
} err = pthread_create(&tid_2,NULL,(void *)ThreadFun,(void *)&t2);
if(err != )
{
printf("error to create thread !\n");
} JoinThread(tid_1);
JoinThread(tid_2); }
else if(type == RWLock)
{
/*rw lock*/
t1.type = RWLock;
t2.type = RWLock;
t3.type = RWLock; err = pthread_create(&tid_1,NULL,(void *)ThreadFun,(void *)&t1);
if(err != )
{
printf("error to create thread !\n");
} err = pthread_create(&tid_2,NULL,(void *)ThreadFun,(void *)&t2);
if(err != )
{
printf("error to create thread !\n");
} err = pthread_create(&tid_3,NULL,(void *)ThreadFun,(void *)&t3);
if(err != )
{
printf("error to create thread !\n");
}
JoinThread(tid_1);
JoinThread(tid_2);
JoinThread(tid_3);
}
else if(type == CondLock)
{
t1.type = CondLock;
err = pthread_create(&tid_1,NULL,(void *)ThreadFun,(void *)&t1);
if(err != )
{
printf("error to create thread !\n");
} sleep();
t2.type = CondLock;
err = pthread_create(&tid_2,NULL,(void *)ThreadFun,(void *)&t2);
if(err != )
{
printf("error to create thread !\n");
}
JoinThread(tid_1);
JoinThread(tid_2); } } void InitMutexLock()
{
if(pthread_mutex_init(&mutex_lock,NULL) != )
{
printf("[Main] error to init mutex lock\n");
}
} void DestoryMutexLock()
{
if(pthread_mutex_destroy(&mutex_lock) != )
{
printf("[Main] error to destory mutex lock\n");
}
} void DestoryRWLock()
{
if(pthread_rwlock_destroy(&rw_lock) != )
{
printf("[Main] error to destroy rw lock \n");
} } void InitRWLock()
{
if(pthread_rwlock_init(&rw_lock,NULL) != )
{
printf("[Main] error to init rw lock\n");
}
} int main(int argc,char **argv)
{
printf("=====================================mutex lock=====================================\n"); InitMutexLock(); CreateThread(MutexLock); DestoryMutexLock(); printf("=====================================rw lock=====================================\n"); InitRWLock(); CreateThread(RWLock); DestoryRWLock(); printf("=====================================Cond lock=====================================\n"); InitMutexLock(); CreateThread(CondLock); DestoryMutexLock();
printf("[Main] quit\n"); return ;
}
运行效果如下:
tiger@ubuntu:/mnt/hgfs/e/Lessons/MyExercise/UtilLibs/THREAD$ ./thread
=====================================mutex lock===================================== ----------------------------------------------------------
[ThreadFun] this is thread
[PrintThreadId] process id : thread id : 0xb6f54b70
[AddCount] thread MutexLock the count is : ----------------------------------------------------------
[ThreadFun] this is thread
[PrintThreadId] process id : thread id : 0xb7755b70
- - - - - - - - - -
----------------------------------------------------------
[DelCount] thread MutexLock the count is :
- [CleanUpFun] clean up : thread second handler
[CleanUpFun] clean up : thread first handler
- - - - - - - - -
---------------------------------------------------------- [JoinThread] catch thread 0xb7755b70 , the return val is [JoinThread] catch thread 0xb6f54b70 , the return val is
=====================================rw lock===================================== ----------------------------------------------------------
[ThreadFun] this is thread
[PrintThreadId] process id : thread id : 0xb6753b70
[PrintCount] thread RWLock the count is :
----------------------------------------------------------
[CleanUpFun] clean up : thread second handler
[CleanUpFun] clean up : thread first handler ----------------------------------------------------------
[ThreadFun] this is thread
[PrintThreadId] process id : thread id : 0xb7755b70
[AddCount] thread RWLock the count is : ----------------------------------------------------------
[ThreadFun] this is thread
[PrintThreadId] process id : thread id : 0xb6f54b70
- - - - - - - - - -
----------------------------------------------------------
[CleanUpFun] clean up : thread second handler
[CleanUpFun] clean up : thread first handler
[DelCount] thread RWLock the count is :
- - - - - - - - - -
---------------------------------------------------------- [JoinThread] catch thread 0xb6f54b70 , the return val is [JoinThread] catch thread 0xb7755b70 , the return val is [JoinThread] catch thread 0xb6753b70 , the return val is
=====================================Cond lock===================================== ----------------------------------------------------------
[ThreadFun] this is thread
[PrintThreadId] process id : thread id : 0xb6753b70 ----------------------------------------------------------
[ThreadFun] this is thread
[PrintThreadId] process id : thread id : 0xb7755b70
[AddCount] thread CondLock the count is :
[AddCount] thread CondLock the count is :
[DelCount] thread CondLock the count is :
---------------------------------------------------------- [JoinThread] catch thread 0xb6753b70 , the return val is
----------------------------------------------------------
[CleanUpFun] clean up : thread second handler
[CleanUpFun] clean up : thread first handler [JoinThread] catch thread 0xb7755b70 , the return val is
[Main] quit
tiger@ubuntu:/mnt/hgfs/e/Lessons/MyExercise/UtilLibs/THREAD$
代码相当拙劣基础,欢迎拍砖。