请先看上一篇:
上次有几处错误在此纠正.
从我现在的测试结果看怎么sleep, usleep, select都可以用在线程中做等待呢???
不解啊!
以下是测试环境和测试结果及源代码>>>
test on redlinux 9.0
linux 2.4.20-8 #1 i686 i686 i386 gnu/linux
-------------------------------------------
use sleep result:
start thread.
thread aaa.16386 wait thread start..,.
thread bbb.16384 no wait start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread bbb.16384 end..,.
thread bbb.16384 no wait start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread bbb.16384 end..,.
thread bbb.16384 no wait start..,.
----------------------------------------
use usleep result:
start thread.
thread aaa.16386 wait thread start..,.
thread bbb.16384 no wait start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread bbb.16384 end..,.
thread bbb.16384 no wait start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread bbb.16384 end..,.
thread bbb.16384 no wait start..,.
-----------------------------------------
use select result:
start thread.
thread aaa.16386 wait thread start..,.
thread bbb.16384 no wait start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread bbb.16384 end..,.
thread bbb.16384 no wait start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread bbb.16384 end..,.
thread bbb.16384 no wait start..,.
----------------------------------------
use pthread_cond_timedwait result:
start thread.
thread aaa.16386 wait thread start..,.
thread bbb.16384 no wait start..,.
thread bbb.16384 end..,.
thread bbb.16384 no wait start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread bbb.16384 end..,.
thread bbb.16384 no wait start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread bbb.16384 end..,.
thread bbb.16384 no wait start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread bbb.16384 end..,.
thread bbb.16384 no wait start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread bbb.16384 end..,.
thread bbb.16384 no wait start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread bbb.16384 end..,.
thread bbb.16384 no wait start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread bbb.16384 end..,.
thread bbb.16384 no wait start..,.
thread aaa.16386 no wait thread end..,.
thread aaa.16386 wait thread start..,.
thread bbb.16384 end..,.
thread bbb.16384 no wait start..,.
##################################################################
#define max_speed 0xffffff
#if 0
static int thr_sleep(const int t)
{
struct timeval t_val;
t_val.tv_sec = t;
t_val.tv_usec = 0;
select(0, null, null, null, &t_val);
return (0);
}
static int thr_sleep(const int t)
{
return (usleep(t * 1000000));
}
pthread_mutex_t pthr_sleep = pthread_mutex_initializer;
pthread_cond_t pthr_cond = pthread_cond_initializer;
static int thr_sleep(const int t)
{
struct timespec ts;
time_t tt;
struct tm *now;
time(&tt);
now = localtime(&tt);
ts.tv_sec = now->tm_sec t;
ts.tv_nsec = 0;
return (pthread_cond_timedwait(&pthr_cond, &pthr_sleep, &ts));
}
#endif
static int thr_sleep(int t)
{
return (sleep(t));
}
static void op_a(void)
{
int i;
while (1)
{
i = max_speed;
printf("thread aaa.%u wait thread start..,.\n", (uint32_t)pthread_self());
while (i--);
thr_sleep(2);
printf("thread aaa.%u no wait thread end..,.\n", (uint32_t)pthread_self());
}
}
static void op_b(void)
{
int i;
while (1)
{
i = max_speed;
printf("thread bbb.%u no wait start..,.\n", (uint32_t)pthread_self());
while (i--);
printf("thread bbb.%u end..,.\n", (uint32_t)pthread_self());
}
}
int main(int argc, char **argv)
{
pthread_t thr;
printf("start thread.\n");
if (pthread_create(&thr, null, (void *)op_a, null))
{
printf("create thread error: %s\n", strerror(errno));
return (-1);
}
op_b();
printf("end thread.\n");
return (0);
}
|