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

全部博文(534)

文章存档

2021年(1)

2019年(1)

2017年(1)

2016年(2)

2013年(2)

2012年(10)

2011年(43)

2010年(10)

2009年(17)

2008年(121)

2007年(253)

2006年(73)

分类: LINUX

2006-10-12 17:45:12

初次写Blog也不知道些什么好,想了好几天,我看就些我的问题吧!

一来可以时常提醒自己这么多问题还没解决, 二来要是有那位前辈看了帮小弟我解决了,那我自当感激...
多谢,多谢!!!
MY E-MAIL: ghk_love(at)163.com
可能部分问题也会慢慢的解决的, 一但我解决了我会在我的Blog上贴上的.

总之人生就是有一个个的问号组成的......

不求最美,但求实用.
----------------------------------------------------

Q1[2006-10-12]:
    在linux C开发的问题,多线程中如何让一个线程等待? 就象多进程中的sleep(seconds).
sleep()是让进程等待的,不应该说等待吧,应该是挂起,如果在线程中用就是让整个进程挂起了噢...
google "pthread sleep"

from:

/* 不错, 可就是select函数功能不怎么会用 */
int mySleep(unsigned int sleepSecond)
{
  timeval        t_timeval;

  t_timeval.tv_sec = sleepSecond;
  t_timeval.tv_usec = 0;

  select( 0, NULL, NULL, NULL, &t_timeval );

  return 0;
}
--------------------------------------------
2006-11-24测试usleep结果同使用select, sleep结果一样, 整个进程处在等待状态.
--------------------------------------------

#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);
}
#endif

static int thr_sleep(const int t)
{
  return usleep(t);
}

static void op_a(void)
{
  int     i = 0xffff;

  while (1)
  {
    printf("Thread.%u wait thread start..,.\n", (uint32_t)pthread_self());
    while (i--);
    thr_sleep(100);

    printf("Thread.%u No wait thread end..,.\n", (uint32_t)pthread_self());
  }
}

static void op_b(void)
{
  int     i = 0xffff;

  while (1)
  {
    printf("Thread.%u No wait start..,.\n",

(uint32_t)pthread_self());
    while (i--);

    printf("Thread.%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);
}

下一篇: http://blog.chinaunix.net/u/19782/showart_205768.html
阅读(2594) | 评论(1) | 转发(0) |
0

上一篇:没有了

下一篇:Unix and Windows

给主人留下些什么吧!~~