📄 draft.c
字号:
/*
this is a pthread testing code.
it initiates two thread, namely, fth and sth.
the fth lock the gMutex,printf ++a, then delay for 2 seconds.
unlock the gMutex, cond_wait for the condition signal
the sth get the gMutex locked, printf --a,cond_timed wait for 1 seconds,
unlock the gMutex.
the PTH_Test function initiates the two threads,
then conditionb signaled after 4 second.
*/
#include "draft.h"
void *fth(void *p)
{
struct timespec time;
time.tv_sec = 2;
time.tv_nsec = 0;
if( pthread_mutex_lock(&gMutex) == PTHR_FAILURE )
{
printf("ERROR: first thread lock gMutex error\n");
}
else
{ __GETSYSTIME( & gtime );
printf("time: %d\n gMutex locked successfully\n",gtime.tv_sec);
printf("++a is %d", a);
pthread_delay_np( &time );
__GETSYSTIME( & gtime );
printf("time: %d thread delay ended\n unlock start\n",gtime.tv_sec);
if( pthread_mutex_unlock(&gMutex) == PTHR_FAILURE )
{
printf("ERROR: first thread unlock mutex error\n");
}
__GETSYSTIME( & gtime );
printf("time:%d first thread begin to wait the cond_signal\n",gtime.tv_sev);
if( pthread_cond_wait(&gCond,&gMutex) == PTHR_FAILURE)
{
printf("ERROR: first thread cond wait error\n");
break;
}
__GETSYSTIME( & gtime );
printf("time:%d cond signaled,first thread cond wait ends\nfth ends",gtime.tv_sev);
}
}
void *sth(void *P)
{
struct timespec CW_time;
CW_time.tv_sec = 3;
CW_time.tv_nsec = 0;
while(1)
{
if( pthread_mutex_lock(&gMutex) == PTHR_FAILURE)continue;
else
{
printf("--a is %d\n",--a);
__GETSYSTIME( & gtime );
printf("time:%d second thread begin the cond timed wait",gtime.tv_sec);
if( pthread_cond_timedwait(&gCond, &gMutex, &CW_time) == PTHR_FAILURE )
{
printf("ERROR: second thread cond time wait error\n");
break;
}
__GETSYSTIME( & gtime );
printf("time:%d second thread end the cond timed wait and begin to unlock the gmutex",gtime.tv_sec);
if( pthread_mutex_unlock(&gMutex) == PTHR_FAILURE )
{
printf("ERROR: Second thread, unlock gmutex error %d\n");
}
else
{
__GETSYSTIME( & gtime );
printf("time: %d in second thread, unlock gmutex successfully\nsth ends",gtime.tv_sec);
}
break;
}
}
}
int PTH_Test()
{
pthread_module_init();
pthread_mutex_init( &gMutex, NULL);
pthread_cond_init(&gCond, NULL);
if(pthread_create(&(id[0]), NULL, fth, (void *)&a, sizeof(int)) == PTHR_FAILURE)
{
printf("create first thread failure\nthe pthread num is %d\n",pthread_get num);
}
else{printf("create first thread success\n");}
pthread_create(&(id[1]), NULL, sth, (void *)&a, sizeof(int)) == PTHR_FAILURE)
{
printf("create second thread failure\nthe pthread num is %d\n",pthread_get num");
}
else{printf("create second thread success\n");}
sleep(4000);
__GETSYSTIME( & gtime );
printf("time:%d pthread condtion signaled",gtime.tv_sec);
if( pthread_cond_signal(&gCond) == PTHR_FAILURE )
{
printf("ERROR: signal cond error\n");
return -1;
}
else{printf("cond signal sent.\n");
pthread_cond_destroy(&gCond);
pthread_mutex_destroy(&gMutex);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -