<div class="gmail_quote">On Mon, Jun 1, 2009 at 1:33 AM, Roman Shmelev <span dir="ltr"><<a href="mailto:rshmelev@gmail.com">rshmelev@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> Async file reading could be done in Windows. What I can give is the<br> link to Russian blog:<br> <a href="http://evgeny-lazin.blogspot.com/2008/12/boostasio.html" target="_blank">http://evgeny-lazin.blogspot.com/2008/12/boostasio.html</a><br> <br> Everything that you described - could be achieved by asio.<br> You can configure number of working threads by running<br> io_service.run() from each of them.<br> I guess, at this point you _will_ need to syncronize access to<br> "controller" through some mutex or asio::strands<br> and it will not significantly lower performance.</blockquote></div><br>So let me see if I understand the basic model correctly.<br><br>In Windows I only have to "think" about 1 thread, which is the main controller thread that runs the event loop.� The event loop looks something like this (pseudocode):<br> <br>int bytes_transferred = 0;<br>event_params params;<br>uint64 next_read_offset = 0;<br>uint64 next_write_offset = 0;<br><br>int outstanding_reads = 0;<br>int outstanding_writes = 0;<br><br>//Fill up the threads with read work<br> for (int i=0; i < max_worker_threads; ++i)<br>{<br>��� async_request(READ_REQUEST, next_read_offset, input_handle, 4096);<br>��� next_read_offset += 4096;<br>}<br><br>while (bytes_transferred < bytes_total)<br>{<br> ���� request what_happened;<br><br>���� block_until_something_happens(&what_happened);<br><br>���� switch (what_happened.code)<br>���� {<br>���� case READ_REQUEST:<br>���������� --outstanding_reads;<br>���������� ++outstanding_writes;<br> ���������� <br>���������� request.type = WRITE_REQUEST;<br>���������� request.offset = next_write_offset;<br>���������� request.handle = output_handle;<br>���������� request.bytes = request.bytes;��� /* write the same number of bytes that were just read */<br> ���������� next_write_offset += request.bytes;<br><br>���������� async_request(&request);<br>���������� break;<br><br>���� case WRITE_REQUEST:<br>���������� --outstanding_writes;<br>���������� bytes_transferred += params.bytes_written;<br> <br>���������� if (bytes_transferred < bytes_total)<br>���������� {<br>� � � ��������� ++outstanding_reads;<br>��������������� request.type = READ_REQUEST;<br>��������������� request.offset = next_read_offset;<br>��������������� request.handle = input_handle;<br> ��������������� request.bytes = 4096;�� /* always read in 4k chunks */<br>��������������� next_read_offset += 4096;<br><br>��������������� async_request(&request);<br>���������� }<br>���� }<br>}<br><br>This way nothing ever has to be synchronized because there is only 1 thread initiating all the reads and writes, what happens in the other threads is completely controlled by the operating system, even the threads are created by the operating system.<br> <br><br>So to achieve something similar with Boost.Asio, my understanding is that:<br><br>a) I can use a standard io_service (don't need to provide a custom implementation) for the controller thread, and 1 additional io_service instance for all worker threads combined.� So a total of 2 io_service instances no matter how many threads I want.<br> <br>b) I create the worker threads manually (so if I want to be able to have 5 outstanding I/O operations spread among reads and writes, I create 5 boost::thread objects, and at the very beginning call service.run() on the single io_service instance used for work.<br> <br>c) In the read_handler() and write_handler() methods that get called on the individual worker threads, I simply call controller_io_service.post(ReadFinished) or controller_io_service.post(WriteFinished).<br><br><br>Would something like this work?� (I could also just try this and _see_ if it works, but I've already been trying it and for some reason the pieces aren't all fitting together in my head).� At what point in this process would I call run() on the master controller io_service?<br>