异步线程间通信
我正在制作一个跨平台程序,嵌入一个小型 RARP 服务器(用 winpcap/pcap 实现)并运行 2 个 TCP IP 服务器。我必须使用C++。
因此,我将至少有 4 个线程,主要线程包括控制器、2 个 TCP/IP 异步套接字和 RARP 服务器。
我计划使用c++ BOOST Asio和Thread,因为我需要在linux和windows XP中运行这个程序。 (而且我不能使用 Qt)
我将执行异步线程间通信。
例如,循环内的火灾事件而不阻塞循环
我该怎么做?最好有便携式图书馆。
谢谢
I'm making a cross plateform program that embbed a small RARP server (implemented with winpcap/pcap) and runs 2 TCP IP servers. I have to use C++.
So i will have at least 4 threads, the main one wich countain the controller, 2 TCP/IP async socket, and the RARP server.
I planned to use c++ BOOST Asio and Thread, because i need to run this program in both linux and windows XP. (and i can't use Qt)
I would perform asynchronous Inter thread communication.
For exemple fire events inside a loop without blocking the loop
How can i do that? With a portable librairy preferably.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对此没有通用的解决方案,您不能只是中断线程并传递要处理的通知。这会导致可怕的重入问题和大量的死锁。仅当线程处于静止状态时才能处理通知。
操作系统通常具有可用的服务来实现这一点。在 Windows 中,这通常是通过将消息发布到消息队列来完成的。由消息循环读取,这是 UI 线程的“空闲”状态。或者通过利用异步过程调用,在线程阻塞时触发并显式允许 APC 运行。
但是您通过需要非特定于平台的解决方案来切断这一点。在这种情况下,你几乎注定要重新发明操作系统功能。您将需要一个线程安全队列,您可以在需要接收通知的线程中轮询该队列。消息队列,由消息循环读取。
There's no generic solution to this, you can't just interrupt a thread and deliver a notification to be processed. That causes horrible re-entrancy problems and large servings of deadlock. A notification can only be processed when the thread is in a quiescent state.
An operating system usually has services available that make it possible. In Windows this is typically done by posting a message to the message queue. Read by the message loop, which is the 'idle' state for a UI thread. Or by leveraging asynchronous procedure calls, fired when the thread is blocking and explicitly allowed APCs to run.
But you cut this off by requiring a non-platform specific solution. In which case you're pretty much doomed to re-invent an OS feature. You'll need a thread-safe queue that you poll in the thread that needs to receive the notification. A message queue, read by a message loop.
看看 ICE 消息传递
它支持进程之间的同步和异步消息传递,无论它们是否在同一进程上节点与否。
有 C++、Obj-C、Java、C#、Python、Ruby 和 PHP 的绑定。
Take a look at ICE messaging
It supports syncrhonous and asyncrounous messaging between processes whether they are on the same node or not.
There are bindings for C++, Obj-C, Java, C#, Python, Ruby, and PHP.