请先看上一篇:
http://blog.chinaunix.net/u/19782/showart_183491.html上次有几处错误在此纠正.
从我现在的测试结果看怎么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);
}
|