
Hi, I'm trying to use asio for a udp client / server arrangment. I started by calling run on the io_service object on a separate thread and the using the async_read_from and the async_send_to. I was calling these from the main application thread. I was getting a lot of these errors that was causing an exception on the io_service run method. First-chance exception at 0x7c812afb in Testbed.exe: Microsoft C++ exception: boost::system::system_error at memory location 0x0012e29c. An invalid argument was supplied This happens after calling the async_send_to method. I've added in a send buffer and started to poll the io_service object instead of running it on it's own thread which has cut down the freqency this is happening however on occations it still does and i'd like to have it running on it's own thread. The question is really should i put in a mutex lock in my sendto function that call's the async_send_to that is unlocked when i recieve the handlesentto callback that's called when the async_send_to is finished. If i do this doesn't this basically make it a blocking function call in the same way as if i call the regular send_to function. There are other options i've thought of but they all seem to result in it basically becoming blocking. I've looked through the examples and there is a lot of different potential choices to make and was looking to see if anyone could advise on an arrangement that would allow for using multiple threads but block as little as possible. Thanks, Tim.

On Sat, 09 May 2009 21:09:47 +0200, Tim Pynegar
[...]The question is really should i put in a mutex lock in my sendto function that call's the async_send_to that is unlocked when i recieve the handlesentto callback that's called when the async_send_to is finished. If i do this doesn't this basically
What happens if you call boost::asio::io_service::run() only in main() and not in a new thread? Your application will then be single-threaded. If there are no crashs then anymore you probably use shared resources in your threads without synchronizing them properly. Boris
[...]

I've looked through the examples and there is a lot of different potential choices to make and was looking to see if anyone could advise on an arrangement that would allow for using multiple threads but block as little as possible.
Lets assume you want to send data from the main thread, while your io_service is running in another thread. You can't access the socket object in a straightforward way from the main thread without potential race-condition, because it's being processed by io_service in another thread. What you can do instead is to bind the data to a functor and post it to io_service's thread (look at the io_service::post() method for details) - this way you don't need to explicitly lock anything.
participants (3)
-
Boris Schaeling
-
Igor R
-
Tim Pynegar