pthread求助

jinhuaxing 2003-05-09 10:11:30
请问当一个线程A在sleep()时,在另一个线程B中能否通过通过什么方法把A唤醒?
...全文
57 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
bwang72 2003-05-13
  • 打赏
  • 举报
回复
清楚了, 谢谢.

jinhuaxing(), 你可以直接用一个MUTEX. 线程A lock/sleep 在这个MUTEX上, 线程B可以通过unlock MUTEX 来把A唤醒.
qqchen79 2003-05-11
  • 打赏
  • 举报
回复
SYNOPSIS
#include <unistd.h>
unsigned int sleep(unsigned int seconds);

DESCRIPTION
sleep() makes the *current process* sleep until seconds sec-
onds have elapsed or a signal arrives which is not
ignored.

RETURN VALUE
Zero if the requested time has elapsed, or the number of
seconds left to sleep.

CONFORMING TO
POSIX.1


qqchen79 2003-05-11
  • 打赏
  • 举报
回复
sorry, my mistake. :">

Unlike other UNIX platforms, Linux threads are processes (they are also created with clone system call, just share resources with each other). So sleep should block the thread only (it's also the process, so from this perspective I'm not wrong :)
lemontel 2003-05-10
  • 打赏
  • 举报
回复
In a multithreaded program, only the invoking thread is suspended from execution.
bwang72 2003-05-10
  • 打赏
  • 举报
回复
在LINUX下, sleep()是作用在整个进程上的吗? 好像只是作用在线程上的. 有没有谁可以确定的讲一下, 谢谢.
qqchen79 2003-05-10
  • 打赏
  • 举报
回复
sleep是作用在整个进程上的,一调sleep,所有的线程都回家睡觉去了,哪里还有谁叫醒谁的问题?:)
如果需要一个单独的线程"sleep"的话,必须自己建一个pthread condition,调用pthread_cond_timedwait。

既然用了condition,其它线程自然可以唤醒它的了。

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧