提高:: ASIO,线程和同步 [英] boost::asio, threads and synchronization

查看:133
本文介绍了提高:: ASIO,线程和同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这在某种程度上关系到<一个href=\"http://stackoverflow.com/questions/4705411/boostasio-io-service-run-vs-poll-or-how-do-i-integrate-boostasio-in-mai\">this问题,但我想我需要知道一点点。我一直试图让我围绕如何做到这一点了几天(而在其他地方工作的)头,但时间已到,我硬着头皮并获得多线程。另外,我比挂钩的问题提供更多的信息后,我。

This is somewhat related to this question, but I think I need to know a little bit more. I've been trying to get my head around how to do this for a few days (whilst working on other parts), but the time has come for me to bite the bullet and get multi-threaded. Also, I'm after a bit more information than the question linked.

首先,关于多线程。正如我一直在测试我的code,我没有与任何多线程困扰。这只是启动到测试服务器的连接,然后一切处理的控制台应用程序。主循环是这样的:

Firstly, about multi-threading. As I have been testing my code, I've not bothered with any multi-threading. It's just a console application that starts a connection to a test server and everything else is then handled. The main loop is this:

while(true)
{
    Root::instance().performIO(); // calls io_service::runOne();
}

当我写我的主要应用,我猜测这个解决方案将不会被接受(因为它会在消息循环当中,虽然有可能,将有问题时调用消息队列块等待消息你可以改变它,这样的消息循环不会阻止,但不是要通过屋顶乱作一团CPU使用率?)

When I write my main application, I'm guessing this solution won't be acceptable (as it would have to be called in the message loop which, whilst possible, would have issues when the message queue blocks waiting for a message. You could change it so that the message-loop doesn't block, but then isn't that going to whack the CPU usage through the roof?)

似乎解决方案是在它扔另一个线程。好的。但后来我读过 :: io_service对象的run()收益没有工作做的时候。那是什么?就是当没有数据,或者没有连接?如果至少一个连接存在它活路?如果是这样,这与其说是一个问题,因为我只需要启动一个新的线程,当第一个连接后,我很高兴,如果这一切停止时有什么事情都没有。我想我通过'没有工作做的定义感到困惑。

The solution it seems is to throw another thread at it. Okay, fine. But then I've read that io_service::run() returns when there is no work to do. What is that? Is that when there's no data, or no connections? If at least one connection exists does it stay alive? If so, that's not so much of a problem as I only have to start up a new thread when the first connection is made and I'm happy if it all stops when there is nothing going on at all. I guess I am confused by the definition of 'no work to do'.

然后,我担心我的同步提升线程与我的主界面线程。所以,我想我的问题是:

Then I have to worry about synchronizing my boost thread with my main GUI thread. So, I guess my questions are:


  1. 什么是使用boost ::在客户端应用程序ASIO关于线程和保持他们活着的最佳实践方法是什么?

  2. 当写从主线程的IO线程的插座,使用是实现同步提高:: ASIO ::后,使呼叫在后来发生io_service对象?

  3. 当接收数据时,人们怎样将数据返回到UI线程?在我使用完成端口的过去,我提出,可以使用:: SendMessage消息发布数据传回主UI线程的特别活动。这不是优雅,但它的工作。

  1. What is the best-practice way of using boost::asio in a client application with regard to threads and keeping them alive?
  2. When writing to a socket from the main thread to the IO thread, is synchronization achieved using boost::asio::post, so that the call happens later in the io_service?
  3. When data is received, how do people get the data back to the UI thread? In the past when I used completion ports, I made a special event that could post the data back to the main UI thread using a ::SendMessage. It wasn't elegant, but it worked.

我会更加今天读了一些,但它会是巨大的,得到一个头从别人谁已经做到了这一点。升压:: ASIO文档不是很大,我的大部分工作至今一直基于比特的文件,一些试验/错误,在网络上的一些例如code的。

I'll be reading some more today, but it would be great to get a heads up from someone who has done this already. The Boost::asio documentation isn't great, and most of my work so far has been based on a bit of the documentation, some trial/error, some example code on the web.

推荐答案

1)看一看<一个href=\"http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/reference/io_service__work.html\">io_service::work.只要一个工作对象存在io_service对象::运行将不会返回。所以如果你开始做你的清理,销毁工作对象,取消任何未完成的操作,例如async_read插座上,等待运行,返回和清理你的资源。

1) Have a look at io_service::work. As long as an work object exists io_service::run will not return. So if you start doing your clean up, destroy the work object, cancel any outstanding operations, for example an async_read on a socket, wait for run to return and clean up your resources.

2)io_service对象::职位将异步运行io_service对象一个线程执行指定的处理程序。回调可以用来获得所执行的操作的结果。

2) io_service::post will asynchronously execute the given handler from a thread running the io_service. A callback can be used to get the result of the operation executed.

3)你需要某种形式的消息系统,通知新的数据你的GUI线程。这里有几种可能性。

3) You needs some form of messaging system to inform your GUI thread of the new data. There are several possibilities here.

至于你对机制的文档的话,我的事情短耳是更好的记录Boost库之一,它带有明显的例子。

As far as your remark about the documention, I thing Asio is one of the better documented boost libraries and it comes with clear examples.

这篇关于提高:: ASIO,线程和同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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