如何随时暂停 pthread? [英] How to pause a pthread ANY TIME I want?

查看:32
本文介绍了如何随时暂停 pthread?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我开始着手将 ucos-ii 移植到 Ubuntu PC.

recently I set out to port ucos-ii to Ubuntu PC.

我们知道,在 ucos-ii 中通过简单地在 pthread 的回调函数中的while"循环中添加一个标志来执行暂停和恢复是不可能的(如下面的解决方案).因为ucos-ii中的进程"可以随时暂停或恢复!

As we know, it's not possible to simulate the "process" in the ucos-ii by simply adding a flag in "while" loop in the pthread's call-back function to perform pause and resume(like the solution below). Because the "process" in ucos-ii can be paused or resumed at any time!

如何休眠或暂停 PThreadLinux 上的 c 语言

我在下面的网站上找到了一种解决方案,但由于它已过时而无法构建.它使用Linux中的进程来模拟ucos-ii中的任务(就像我们Linux中的进程一样).

I have found one solution on the web-site below, but it can't be built because it's out of date. It uses the process in Linux to simulate the task(acts like the process in our Linux) in ucos-ii.

http://www2.hs-esslingen.de/~zimmerma/software/index_uk.html

如果pthread可以像进程一样可以随时暂停和恢复,请告诉我一些相关的功能,我可以自己解决.如果不能,我想我应该专注于旧的解决方案.非常感谢.

If pthread can act like the process which can be paused and resumed at any time, please tell me some related functions, I can figure it out myself. If it can't, I think I should focus on the older solution. Thanks a lot.

推荐答案

如果用条件变量在特定点停止是不够的,那么你不能用 pthreads 做到这一点.pthread 接口不包括挂起/恢复功能.

If stopping at specific points with a condition variable is insufficient, then you can't do this with pthreads. The pthread interface does not include suspend/resume functionality.

例如,请参阅答案 E.4 此处:

See, for example, answer E.4 here:

POSIX 标准没有提供线程 A 可以在没有 B 合作的情况下暂停另一个线程 B 的执行的机制.实现暂停/重新启动机制的唯一方法是让 B 定期检查某个全局变量是否暂停请求,然后在条件变量上挂起自身,稍后另一个线程可以发出信号以重新启动 B.

The POSIX standard provides no mechanism by which a thread A can suspend the execution of another thread B, without cooperation from B. The only way to implement a suspend/restart mechanism is to have B check periodically some global variable for a suspend request and then suspend itself on a condition variable, which another thread can signal later to restart B.

该常见问题解答继续描述了几种非标准方法,一种在 Solaris 中,另一种在 LinuxThreads 中(现在已过时;不要将其与 Linux 上的当前线程混淆);这些都不适用于您的情况.

That FAQ answer goes on to describe a couple of non-standard ways of doing it, one in Solaris and one in LinuxThreads (which is now obsolete; do not confuse it with current threading on Linux); neither of those apply to your situation.

这篇关于如何随时暂停 pthread?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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