ASIO HTTP Server 3 Example: sharing stack data between threads

I was reading through the ASIO HTTP Server 3 example (http://tinyurl.com/35hwtd5) that uses a thread pool to call io_service::run. In both posix_main.cpp and win_main.cpp an http::server3::server object is created on the stack in main(). server has a boost::asio::io_service member (io_service_). main calls server::run, which creates a pool of threads that each call io_service_.run. So at this point the io_service_ object is within main's stack is being shared by multiple threads. Am I missing something or is this a bad thing to do and encourage others to copy? Seems like a simple fix would be to allocate the server object in main on the heap. Thanks, Aaron Riekenberg

AMDG Aaron Riekenberg wrote:
I was reading through the ASIO HTTP Server 3 example (http://tinyurl.com/35hwtd5) that uses a thread pool to call io_service::run.
In both posix_main.cpp and win_main.cpp an http::server3::server object is created on the stack in main(). server has a boost::asio::io_service member (io_service_). main calls server::run, which creates a pool of threads that each call io_service_.run. So at this point the io_service_ object is within main's stack is being shared by multiple threads.
The threads can all access the memory fine, and the io_service object won't be cleaned up until all the threads are joined. There is no problem here.
Am I missing something or is this a bad thing to do and encourage others to copy? Seems like a simple fix would be to allocate the server object in main on the heap.
In Christ, Steven Watanabe
participants (2)
-
Aaron Riekenberg
-
Steven Watanabe