什么意思了POSIX线程是"暂停"? [英] What does it mean to POSIX that a thread is "suspended"?

查看:122
本文介绍了什么意思了POSIX线程是"暂停"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上最近的问题,子公司的问题出现了关于什么点与可取消一个pthreads的线程取消请求 PTHREAD_CANCEL_DEFERRED 可以预期到时采取行动。引用标准并有点律师业接踵而至。我没有太多关注特别是关于我是否是在我对这个问题的意见错了,但我的将会的想证实我理解正确POSIX的规定。

In the course of commentary on a recent question, a subsidiary question arose about at what point a cancellation request for a pthreads thread with cancelability PTHREAD_CANCEL_DEFERRED can be expected to be acted upon. References to the standard and a bit of lawyering ensued. I'm not much concerned specifically about whether I was mistaken in my comments on that question, but I would like to be sure I understand POSIX's provisions correctly.

该标准的最相关的部分说:

The most pertinent section of the standard says

每当一个线程已启用可取消和取消请求已经取得了与线程作为目标,然后线程调用是取消点的任何功能[...],取消请求,应在之前采取行动函数返回。如果一个线程已经启用可取消并取消请求与线程作为目标提出,而线程暂停在取消点,线程将被唤醒,并取消请求须在采取行动。

Whenever a thread has cancelability enabled and a cancellation request has been made with that thread as the target, and the thread then calls any function that is a cancellation point [...], the cancellation request shall be acted upon before the function returns. If a thread has cancelability enabled and a cancellation request is made with the thread as a target while the thread is suspended at a cancellation point, the thread shall be awakened and the cancellation request shall be acted upon.

什么,但是,它意味着一个线程被暂停? POSIX明确定义过程的术语,但不,据我能确定,线程。另一方面,POSIX文档螺纹悬浮成为功能,包括少数的行为中,但不限于,其中的一些有关的同步对象。如果一个然后得出结论,这些服务统称为期限的相关定义是什么?

What, though, does it mean for a thread to be "suspended"? POSIX explicitly defines the term for processes, but not, as far as I can determine, for threads. On the other hand, POSIX documents thread suspension to be among the behaviors of a handful of functions, including, but not limited to, some of those related to synchronization objects. Should one then conclude that those serve collectively as the relevant definition of the term?

而作为这一切都属于鉴于POSIX不指定线程挂起为)的行为读的一部分(即催生了这一调查线索的问题, FREAD(),或任何一般文件或流I / O功能,如果一个线程没有在帐户被锁定在我取得进展/ O,是否一定意味着它是暂停注销的目的是什么?

And as this all pertains to the question that spawned this line of inquiry, given that POSIX does not specify thread suspension as part of the behavior of read(), fread(), or any of the general file or stream I/O functions, if a thread is not making progress on account of being blocked on I/O, does that necessarily mean it is "suspended" for the purposes of cancellation?

推荐答案

一个挂起的线程是一个,就像你说的,被封锁在一个套接字读取,等待信号灯变为可用,等等。

A suspended thread is one that, as you say, is blocked on a socket read, waiting for a semaphore to become available, etc.

,它可能是在code依靠消除也就是移植比它的价值可能会比较麻烦。

Given that POSIX implementations vary at the tricky edges, and that there is the potential for a thread to be blocked in a function that is not a cancellation point, it might be that relying on cancellation in code that is to be ported might be more trouble than it's worth.

我从来没有使用过它,我总是选择有code明确指示线程终止(通常是下降的消息管道或队列)。这是很容易用通信顺序进程或角色模型系统。

I've never used it, I've always chosen to have code to explicitly instruct a thread to terminate (normally a message down a pipe or queue). This is very easy with a Communicating Sequential Processes or Actor Model system.

这样清理可以自己控制下等必要进行,释放存储器。我不知道一个被取消的线程是否会清理它的内存(我不怀疑),或者是否有一个at_exit()式的东西选项(可能有)。整体来说,我认为,如果只有一个单一的方式一个线程可以退出应用程序的行为更彻底的控制。

That way clean up can be done under one's own control, freeing memory, etc. as necessary. I've no idea whether a cancelled thread will clean up its memory (I suspect not), or whether there is the option for an at_exit() type thing (there may be). On the whole I think that application behaviour is more thoroughly controlled if there is only one single way a thread can exit.

== ==编辑

@JohnBollinger,

@JohnBollinger,

所使用的语言如果一个线程可取消启用并取消请求与线程作为目标提出,而线程暂停在取消点可能是跨$如果一个线程使可取消p $ ptted为键,如果取消和IF暂停执行线程阻塞,如果线程被封锁该线程将被唤醒... 。换言之,他们留给了POSIX子系统的实现者。

The language used If a thread has cancelability enabled and a cancellation request is made with the thread as a target while the thread is suspended at a cancellation point could be interpretted as IF a thread has cancelability enabled AND IF cancelled and IF implementation suspends blocked threads AND IF the thread is blocked THEN the thread shall be awakened.... In other words, they're leaving it up to the implementer of the POSIX subsystem.

Cygwin的实施选择()不(或至少没有)导致线程被挂起。取而代之的是,每个文件描述一个轮询线程测试signalable活动,由于缺乏基本的Windows中过像选择()(它得到接近,但没有雪茄。WIN32选择()的作品只插座)。选择的()实现早在上世纪80年代经常工作也是这样的。

Cygwin's implementation of select() does not (or at least did not) result in the thread being suspended. Instead it spawns a polling thread per file descriptor to test for signalable activity, due to the fundamental lack of anything quite like select() in Windows (it gets close, but no cigar. Win32 select() works on only sockets). Implementations of select() back in the 1980s often worked this way too.

这可能是像这样的POSIX不愿意当一个线程被挂起明确定义的原因。历史上很多的选择实现()是这样的,使其成为一个雷区标准委员会说,当一个线程可能会或可能不会被暂停。当然,造成选择的复杂性()也将适用于一个过程,但作为POSIX确实定义暂停过程中,它似乎奇怪,他们不能/没有定义扩展到线程。

It might be for reasons like this that POSIX is reluctant to clearly define when a thread is suspended. Historically many implementations of select() were like this, making it a minefield for a standards committee to say when a thread might or might not be suspended. Of course the complexities caused by select() would also apply to a process but as POSIX does define a suspended process it does seem odd that they couldn't / didn't extend the definition to threads.

这可能是下降到线程如何实施;可以想见的是不使用操作系统线程(有点像ADA的在天回早期的实现时的操作系统根本没有做线程)POSIX的实施,并在这样的实施阻塞的线程可能不会暂停(在不采取CPU周期的意义上)都没有。

It might be down to how threads are implemented; you can conceivably have a POSIX implementation that doesn't use OS threads (a bit like the early implementations of ADA back in the days when OSes didn't do threads at all), and in such an implementation a blocked thread might not be suspended (in the sense of taking no CPU cycles) at all.

这篇关于什么意思了POSIX线程是"暂停"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆