📄 test2.c
字号:
/* test glb_mutex lock and unlock
main fun create two thr, sleep 4 sec, send a cond signal
cld1 lock a mutex, acc a var, sleep 2 sec, unlock the mutex
cld2 try to lock the mutex, if lock succ, wait for a cond signal (or timer wait of 1 sec), acc a var, unlock the mutex
*/
#include "test_i.h"
/* glb var def */
int a;
/* fun */
void *cld1_2(void *p)
{
printf("\nEntering cld1\n");
if( pthread_mutex_lock(&glb_mutex) == PTHR_FAILURE )
{
printf("ERROR: lock glb_mutex error\n");
}
else
{
__GETSYSTIME( & glb_time );
printf("\nLock glb_mutex, time: %d\n",glb_time.tv_sec);
printf("in cld1: a += 6, a = %d, time: %d\n",a,glb_time.tv_sec);
printf("in cld1: sleep 2 ....\n");
wait_time.tv_sec = 2;
wait_time.tv_nsec = 0;
pthread_delay_np(&wait_time);
if( pthread_mutex_unlock(&glb_mutex) == PTHR_FAILURE )
{
__GETSYSTIME( & glb_time );
printf("ERROR: in cld1, unlock glb_mutex error, time: %d\n",glb_time.tv_sec);
}
else
{
__GETSYSTIME( & glb_time );
printf("in cld1, unlock glb_mutex succ, time: %d\n",glb_time.tv_sec);
}
}
}
void *cld2_2(void *p)
{
printf("\nEntering cld2\n");
while(1){
if( pthread_mutex_lock(&glb_mutex) == PTHR_FAILURE )
{
continue;
}
else
{
struct timespec cond_wait_time;
__GETSYSTIME( & glb_time );
printf("in cld2: lock glb_mutex succ, time: %d\n",glb_time.tv_sec);
/*
/* wait with timer
cond_wait_time.tv_sec = 1;
cond_wait_time.tv_nsec = 0;
if( pthread_cond_timedwait(&glb_cond, &glb_mutex, &cond_wait_time) == PTHR_FAILURE )
{
printf("ERROR: in cld1, cond time wait error\n");
break;
}
*/
/* wait without timer */
if( pthread_cond_wait(&glb_cond,&glb_mutex) == PTHR_FAILURE)
{
printf("ERROR: in cld1, cond wait error\n");
break;
}
__GETSYSTIME( & glb_time );
printf("in cld2: resume, time: %d\n",glb_time.tv_sec);
printf("in cld2: a += 6, a = %d, time: %d\n",a,glb_time.tv_sec);
if( pthread_mutex_unlock(&glb_mutex) == PTHR_FAILURE )
{
__GETSYSTIME( & glb_time );
printf("ERROR: in cld2, unlock glb_mutex error, time: %d\n",glb_time.tv_sec);
}
else
{
__GETSYSTIME( & glb_time );
printf("in cld2, unlock glb_mutex succ, time: %d\n",glb_time.tv_sec);
}
break;
}
}
}
int test2( void )
{
pthread_t id[2];
printf("\n\nEntering test2 ....\n");
ptherad_module_init();
/* glb_mutex = PTHREAD_MUTEX_INITIALIZER; */
pthread_mutex_init( &glb_mutex, NULL);
pthread_cond_init(&glb_cond, NULL);
a = 4;
if( pthread_create(&(id[0]), NULL, cld1_2, (void *)&a, sizeof(int)) == PTHR_FAILURE)
{
printf("ERROR: create cld1 thr error\n");
return -1;
}
if( pthread_create(&(id[1]), NULL, cld2_2, (void *)&a, sizeof(int)) == PTHR_FAILURE)
{
printf("ERROR: create cld2 thr error\n");
return -1;
}
wait_time.tv_sec = 6;
wait_time.tv_nsec = 0;
__GETSYSTIME( & glb_time );
printf("delay 4 ...., time: %d\n",glb_time.tv_sec);
pthread_delay_np(&wait_time);
if( pthread_cond_signal(&glb_cond) == PTHR_FAILURE )
{
printf("ERROR: signal cond error\n");
return -1;
}
__GETSYSTIME( & glb_time );
printf("cond signal send, time: %d\n",glb_time.tv_sec);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -