简体   繁体   English

如何立即暂停一个线程?

[英]How immediately pause a thread?

Assum have two thread that one of them has more priority and they are running on same core (single core), I only want work just one thread at same time.(maybe you say that is not threading paradigm, but actually I just made my problem minimal here)假设有两个线程,其中一个具有更高的优先级,并且它们在同一个核心(单核)上运行,我只想同时工作一个线程。(也许你说这不是线程范例,但实际上我只是做了我的这里的问题最小)

T1 ~~~e1~e2~e3~e4~...~~~~~~~eK~~~~~~~~...~~~ eN~~~~~ ///(e1...eN)packed as a task.
                |            |
T2 ~~~~~~~~~~~~pause~~~~~~continue~~~~~~~~~~~~~~~~~~ ///(pause & continue)is just title time for T1 that T2 is operating (T2 has more priority).

**~** is time, and **e** is expressions that is evaluate there. **~**是时间, **e**是在那里求值的表达式。 and whole of e1,e2,... is one function that is api caller function(task), So I want just pause T1 there( ~pause~ ) and run my T2 until it's finished,and when finished continue T1.整个 e1,e2,... 是一个函数,它是 api 调用函数(任务),所以我只想在那里暂停 T1( ~pause~ )并运行我的 T2 直到它完成,完成后继续 T1。

Note: I could not changed **e** job (function).注意:我无法更改**e**工作(功能)。

What I know?我知道的?

creating conditional_variable (CV) and when finished T2 notify CV to wake up, but it is not my achievement because I want make T1 exactly pause in the e4 (pause time) immediately and continue T2 until it's finished(or my continue time).创建conditional_variable (CV) 并在完成时通知 CV 唤醒,但这不是我的成就,因为我想让 T1 立即在e4 (暂停时间)中暂停并继续 T2 直到它完成(或我的继续时间)。

my knowledge is same as: https://en.cppreference.com/w/cpp/thread/condition_variable#Example Do we have any thread::method that pause immediately(force context switch)?(I dont mean yield !)我的知识与以下内容相同: https ://en.cppreference.com/w/cpp/thread/condition_variable#Example 我们是否有任何 thread::method 立即暂停(强制上下文切换)?(我不是说屈服!)

C++ has no way of facilitating such a thing. C++ 没有办法促进这样的事情。 You don't have that level of control over threads.您对线程没有那种级别的控制。

On the OS level, that's what thread priorities are for.在操作系统级别,这就是线程优先级的用途。 A higher-priority thread being ready should always be scheduled before a lower-priority thread.准备就绪的高优先级线程应始终在低优先级线程之前调度。 So you can use OS APIs to change the thread priorities for your threads, and should get approximately the right result - note, though, that this is approximate, because you have no guarantee the OS will immediately trigger a task switch on T2 getting ready.因此,您可以使用操作系统 API 来更改线程的线程优先级,并且应该得到大致正确的结果 - 但请注意,这是近似值,因为您无法保证操作系统会在 T2 准备就绪时立即触发任务切换。

Overall, the whole thing sounds like an XY problem, and you would be better served by looking at the underlying problem and thinking of better ways of solving it.总的来说,整个事情听起来像是一个 XY 问题,通过查看潜在问题并思考更好的解决方法,您会得到更好的服务。

I think you may be overthinking the problem you're trying to solve.我认为您可能对您要解决的问题想得太多了。

If you have (literally any) modern multitasking operating system (such as Linux, Windows, etc), and you make T2 a higher thread priority than T1, that operating system will preferentially run T2 ahead of T1.如果你有(几乎任何)现代多任务操作系统(如 Linux、Windows 等),并且你使 T2 的线程优先级高于 T1,则该操作系统将优先运行 T2,然后再运行 T1。 The only circumstance under which it will run T1 at all is if there is some reason for the OS to suspend T2, eg it's either got blocked on I/O, or it's waiting for a semaphore, etc. (if we're assuming a single core machine).它运行 T1 的唯一情况是操作系统是否有某种原因暂停 T2,例如它在 I/O 上被阻塞,或者它正在等待信号量等(如果我们假设单核机)。

More or less the whole idea of a modern OS is to prevent the running of one thread or process influencing the running of another thread or process, unless they're written to cooperate somehow using synchronisation objects such as semaphores, condition variables, IPC, etc. So, you cannot achieve what you're wanting to do unless you can change e to use such mechanisms to allow T1 and T2 to cooperate.现代操作系统的整体理念或多或少是防止一个线程或进程的运行影响另一个线程或进程的运行,除非它们被编写为使用信号量、条件变量、IPC 等同步对象以某种方式协作. 因此,除非您可以更改e以使用此类机制允许 T1 和 T2 进行合作,否则您将无法实现您想要做的事情。 T2 will not be able to do it all by itself. T2 无法独自完成这一切。

About the only thing that T2 can do is to use timers; T2 唯一能做的就是使用定时器; ie it yields for a period of time, after which the OS will reschedule (resume) T2.即它会产生一段时间,之后操作系统将重新安排(恢复)T2。 But you can't get that aligned with the execution of e() in T1.但是您无法使它与 T1 中 e() 的执行保持一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM