[asio] Question regarding io_service.post() and pool of threads

Hi, I was looking at this website where the guy gives an interesting example about the use of a pool of threads and io_service.post(): http://mostlycoding.blogspot.com/2009/05/asio-library-has-been-immensely-hel... However, something confuses me a bit, cf. the website "In just a bunch of lines of code we have a thread pool executing any arbitrary task whenever one thread is available. No need to address the usual multi-threading issues with concurrent jobs sharing few resources: just post what you need and let io_service take care of it!" Extracted from Boost Asio reference: "The io_service<http://www.boost.org/doc/libs/1_44_0/doc/html/boost_asio/reference/io_service.html> guarantees that the handler will only be called in a thread in which the run(), run_one (), poll() or poll_one() member functions is currently being invoked." For my understanding, since there is a pool of threads, a handler can run concurrently in different threads, even though we are using io_service.post(). Is that correct? My concern here is about the need or not to lock the data accessed by the handler. Thank you. Regards, Romain

Since shared objects are safe( except when calling reset() prematurely), I believe that there is no need to use a lock/semaphore. ~ Vivek Prakash On Tue, Sep 28, 2010 at 8:48 AM, Romain CHANU <romainchanu@gmail.com> wrote:
Hi,
I was looking at this website where the guy gives an interesting example about the use of a pool of threads and io_service.post():
http://mostlycoding.blogspot.com/2009/05/asio-library-has-been-immensely-hel...
However, something confuses me a bit, cf. the website "In just a bunch of lines of code we have a thread pool executing any arbitrary task whenever one thread is available. No need to address the usual multi-threading issues with concurrent jobs sharing few resources: just post what you need and let io_service take care of it!"
Extracted from Boost Asio reference: "The io_service< http://www.boost.org/doc/libs/1_44_0/doc/html/boost_asio/reference/io_servic...
guarantees that the handler will only be called in a thread in which the run(), run_one (), poll() or poll_one() member functions is currently being invoked."
For my understanding, since there is a pool of threads, a handler can run concurrently in different threads, even though we are using io_service.post().
Is that correct? My concern here is about the need or not to lock the data accessed by the handler.
Thank you.
Regards,
Romain _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Hi Vivek, I did a simple program that creates a pool of threads running io_service.run() and calling a function many times (through io_service.post() ) (like the simple program in the website given earlier). My function accesses / modifies an object (without lock) and the program just crashes. I am not really sure of the behavior of functions posted to io_service (i.e io_service.post() ) with multiple threads running io_service.run(). Anyone? On 28 September 2010 14:21, Vivek Prakash <vivek.cs.iitr@gmail.com> wrote:
Since shared objects are safe( except when calling reset() prematurely), I believe that there is no need to use a lock/semaphore.
~ Vivek Prakash
On Tue, Sep 28, 2010 at 8:48 AM, Romain CHANU <romainchanu@gmail.com> wrote:
Hi,
I was looking at this website where the guy gives an interesting example about the use of a pool of threads and io_service.post():
http://mostlycoding.blogspot.com/2009/05/asio-library-has-been-immensely-hel...
However, something confuses me a bit, cf. the website "In just a bunch of lines of code we have a thread pool executing any arbitrary task whenever one thread is available. No need to address the usual multi-threading issues with concurrent jobs sharing few resources: just post what you need and
let
io_service take care of it!"
Extracted from Boost Asio reference: "The io_service<
http://www.boost.org/doc/libs/1_44_0/doc/html/boost_asio/reference/io_servic...
guarantees that the handler will only be called in a thread in which the run(), run_one (), poll() or poll_one() member functions is currently being invoked."
For my understanding, since there is a pool of threads, a handler can run concurrently in different threads, even though we are using io_service.post().
Is that correct? My concern here is about the need or not to lock the data accessed by the handler.
Thank you.
Regards,
Romain _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

On 2010-09-28 10:24, Romain CHANU wrote:
Hi Vivek,
I did a simple program that creates a pool of threads running io_service.run() and calling a function many times (through io_service.post() ) (like the simple program in the website given earlier).
My function accesses / modifies an object (without lock) and the program just crashes.
I am not really sure of the behavior of functions posted to io_service (i.e io_service.post() ) with multiple threads running io_service.run().
Anyone?
if the io_service is running in a threadpool, handlers that are posted may execute at any time from any thread. If you would like to run handlers which access shared resources that need protection, you could consider looking into Asio's strand facility. Rutger
participants (3)
-
Romain CHANU
-
Rutger ter Borg
-
Vivek Prakash