如何挂起另一个线程(不是当前线程)? [英] How do I suspend another thread (not the current one)?

查看:453
本文介绍了如何挂起另一个线程(不是当前线程)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现微控制器的仿真.这种仿真并不是要对一个特定的微控制器进行时钟周期的精确表示,而是要检查代码的一般正确性.

I'm trying to implement a simulation of a microcontroller. This simulation is not meant to do a clock cycle precise representation of one specific microcontroller but check the general correctness of the code.

我想到了让一个主线程"执行普通代码,让另一个线程执行ISR代码.每当需要运行ISR时,ISR线程就会挂起主线程".

I thought of having a "main thread" executing normal code and a second thread executing ISR code. Whenever an ISR needs to be run, the ISR thread suspends the "main thread".

当然,我想具有阻止中断的功能. 我想用互斥锁解决这个问题,只要ISR线程执行ISR代码,ISR线程就会保留,而只要中断被阻止",主线程就会保留它.

Of course, I want to have a feature to block interrupts. I thought of solving this with a mutex that the ISR thread holds whenever it executes ISR code while the main thread holds it as long as "interrupts are blocked".

然后可以通过挂起并杀死主线程(并启动一个执行POR功能的新线程)来实现POR(上电复位).

A POR (power on reset) can then be implemented by not only suspending but killing the main thread (and starting a new one executing the POR function).

Windows API提供了必要的功能. 但是用posix线程(在Linux上)执行上述操作似乎是不可能的.

The windows API provides the necessary functions. But it seems to be impossible to do the above with posix threads (on linux).

我不想更改实际的独立于硬件的微控制器代码.因此,插入任何东西来检查未决的中断都不是可行的选择.

I don't want to change the actual hardware independent microcontroller code. So inserting anything to check for pending interrupts is not an option.

在性能不佳的地方接收中断是可取的,因为这在微控制器上也会发生(除非阻止中断).

Receiving interrupts at non well behaved points is desirable, as this also happens on microcontrollers (unless you block interrupts).

有没有办法在Linux上挂起另一个线程? (我认为调试器必须以某种方式使用该选项.)

Is there a way to suspend another thread on linux? (Debuggers must use that option somehow, I think.)

请不要告诉我这是一个坏主意.我知道在大多数情况下都是如此.但是主代码不使用标准库或锁/互斥锁/信号灯.

Please, don't tell me this is a bad idea. I know that is true in most circumstances. But the main code does not use standard libs or lock/mutexes/semaphores.

推荐答案

我认为以某种方式发送其他线程SIGSTOP是可行的.

Somehow I think sending the other thread SIGSTOP works.

但是,最好编写一些涉及senaogires.mutexes和全局变量的线程通信.

However, you are far better off writing some thread communication involving senaogires.mutexes and global variables.

您看到的是,如果在malloc()中挂起另一个线程并调用malloc()->死锁.

You see, if you suspend the other thread in malloc() and you call malloc() -> deadlock.

我是否提到过很多C标准库函数(更不用说您使用的其他库了)会在后面调用malloc()?

Did I mention that lots of C standard library functions, let alone other libraries you use, will call malloc() behind your back?

嗯,没有标准库代码.也许可以使用信号处理程序中的setjmp/longjump()来模拟POR,并使用信号处理器来模拟中断.

Hmmm, no standard library code. Maybe use setjmp/longjump() from signal handler to simulate the POR and a signal handier to simulate interrupt.

要对此保持批评:EDIT之后的内容被接受了答案,这是一个特定的方案,不能在任何其他方案中使用.

TO THOSE WHO KEEP DOWNVOTING THIS: The answer was accepted for the contents after EDIT, which is a specific scenario that cannot be used in any other scenario.

这篇关于如何挂起另一个线程(不是当前线程)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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