线程间通信.如何向另一个线程发送信号 [英] Inter-thread communication. How to send a signal to another thread

查看:449
本文介绍了线程间通信.如何向另一个线程发送信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有两个线程

In my application I have two threads

  1. 大部分时间都很忙的主线程"
  2. 一个附加线程",它发出一些HTTP请求并阻塞直到得到响应.

但是,HTTP响应只能由主线程处理,因为它依赖于它的线程本地存储和非线程安全功能.

However, the HTTP response can only be handled by the main thread, since it relies on it's thread-local-storage and on non-threadsafe functions.

我正在寻找一种方法来告诉主线程何时接收到HTTP响应以及相应的数据.主线程应被附加线程中断,并尽快处理HTTP响应,然后从之前被中断的地方继续工作.

I'm looking for a way to tell the main thread when a HTTP response was received and the corresponding data. The main thread should be interrupted by the additional thread and process the HTTP response as soon as possible, and afterwards continue working from the point where it was interrupted before.

  • 我可以想到的一种方法是,其他线程使用SuspendThread挂起主线程,使用某些内联汇编器从主线程复制TLS,执行响应处理功能本身并恢复主线程之后.

  • One way I can think about is that the additional thread suspends the main thread using SuspendThread, copies the TLS from the main thread using some inline assembler, executes the response-processing function itself and resumes the main thread afterwards.

我认为的另一种方法是,在第二个线程回调例程的某个特定地址上设置一个断点,以便在第二个线程指令指针移至该断点时通知主线程-因此-已收到HTTP响应.

Another way in my thoughts is, setting a break point onto some specific address in the second threads callback routine, so that the main thread gets notified when the second threads instruction pointer steps on that break point - and therefore - has received the HTTP response.

但是,这两种方法似乎都不是很好,即使只是考虑它们,它们也会受到伤害,并且它们看起来并不可靠.

However, both methods don't seem to be nicely at all, they hurt even if just thinking about them, and they don't look really reliable.

我可以用来中断我的主线程,说这应该礼貌并在执行其他操作之前先处理HTTP响应吗?不依赖库的答案是值得赞赏的,但如果它提供了一些不错的解决方案,我也会考虑依赖.

What can I use to interrupt my main thread, saying it that it should be polite and process the HTTP response before doing anything else? Answers without dependencies on libraries are appreciated, but I would also take some dependency, if it provides some nice solution.

跟随问题 (关于QueueUserAPC解决方案)得到了回答,并解释说,在我的情况下,没有安全的方法来执行 push 行为.

推荐答案

这可能是其中一种情况,在这种情况下,人们自己重新考虑了一个非常具体的想法,而没有重新考虑大局.没有单一机制可以使单个线程在当前上下文中停止执行,执行其他操作并在中断的确切行继续执行.如果可能的话,这将打败最初拥有线程的目的.正如您已经提到的那样,在不退缩和重新考虑整体体系结构的情况下,最优雅的选择似乎是使用另一个线程来等待HTTP响应,将其挂起在安全的地方,自行处理响应,然后恢复主线程.在这种情况下,您可能会重新考虑线程本地存储是否仍然有意义,或者范围更广的范围是否更合适,因为每次中断主线程时,您可能会浪费很多周期来复制它.

This may be one of those times where one works themselves into a very specific idea without reconsidering the bigger picture. There is no singular mechanism by which a single thread can stop executing in its current context, go do something else, and resume execution at the exact line from which it broke away. If it were possible, it would defeat the purpose of having threads in the first place. As you already mentioned, without stepping back and reconsidering the overall architecture, the most elegant of your options seems to be using another thread to wait for an HTTP response, have it suspend the main thread in a safe spot, process the response on its own, then resume the main thread. In this scenario you might rethink whether thread-local storage still makes sense or if something a little higher in scope would be more suitable, as you could potentially waste a lot of cycles copying it every time you interrupt the main thread.

这篇关于线程间通信.如何向另一个线程发送信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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