Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4008549
  • 博文数量: 536
  • 博客积分: 10470
  • 博客等级: 上将
  • 技术积分: 4825
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-26 14:08
文章分类

全部博文(536)

文章存档

2024年(3)

2021年(1)

2019年(1)

2017年(1)

2016年(2)

2013年(2)

2012年(10)

2011年(43)

2010年(10)

2009年(17)

2008年(121)

2007年(252)

2006年(73)

分类: C/C++

2006-11-24 18:43:34

请先看上一篇:
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);
}

阅读(3128) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~