为什么在winpthreads的'纳米leep”实现中多次调用`pthread_testcancel`?
nanosleep
winpthreads的实现,它是Windows的POSIX线程的端口,在其实现中多次调用pthread_testcancel
。
nanosleep
in [ nanosleep.c ]调用pthread_delay_np_ms
未导出并进行实际睡眠。这是pthread_delay_np_ms
in [ thread.c ]。
int
pthread_delay_np_ms (DWORD to)
{
struct _pthread_v *s = __pthread_self_lite ();
if (!to)
{
pthread_testcancel ();
Sleep (0);
pthread_testcancel ();
return 0;
}
pthread_testcancel ();
if (s->evStart)
WaitForSingleObject (s->evStart, to);
else
Sleep (to);
pthread_testcancel ();
return 0;
}
您可以看到PTHREAD_TESTCANCEL
由于某种原因被称为多次。
我认为这可能与以下内容一样简单的额外代码的原因是什么?
int
pthread_delay_np_ms (DWORD to)
{
Sleep (to);
return 0;
}
pthread_testcancel
实现什么?另外,一个问题是为什么waitforsingleobject
优先于睡眠
当可用时可用?
The nanosleep
implementation of winpthreads, which is a port of POSIX threads to Windows, calls pthread_testcancel
multiple times in its implementation.
nanosleep
in [nanosleep.c] calls pthread_delay_np_ms
which is not exported and does the actual sleeping. This is the code of pthread_delay_np_ms
in [thread.c].
int
pthread_delay_np_ms (DWORD to)
{
struct _pthread_v *s = __pthread_self_lite ();
if (!to)
{
pthread_testcancel ();
Sleep (0);
pthread_testcancel ();
return 0;
}
pthread_testcancel ();
if (s->evStart)
WaitForSingleObject (s->evStart, to);
else
Sleep (to);
pthread_testcancel ();
return 0;
}
You can see pthread_testcancel
is called multiple times for some reason.
What is the reason for the additional code while I think this could be as simple as follows?
int
pthread_delay_np_ms (DWORD to)
{
Sleep (to);
return 0;
}
What does pthread_testcancel
achieve? Also, a side question is why is WaitForSingleObject
preferred over Sleep
when a dummy event handle is available?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
另一个线程可以调用
pthread_cancel
在这种情况下waitforsingleobject
将立即返回,并且在您的简单睡眠
时将杀死该线程在未知的时间内。为什么他们在
睡觉(0)
和之前调用
,我不知道。也许您应该直接询问作者是否在乎。我认为他们在那里是有原因的...pthread_testcancel
,如果(s-> evstart)Another thread can call
pthread_cancel
in which caseWaitForSingleObject
will return immediately and the thread will be killed while your simpleSleep
would keep a now pointless thread around for an unknown amount of time.Why they are calling
pthread_testcancel
beforeSleep(0)
andif (s->evStart)
, I don't know. Maybe you should ask the authors directly if you care that much about it. I assume they are there for a reason...