Discussion:
[asio-users] async_read in session fetches wrong data
mm.w
2016-02-08 17:21:23 UTC
Permalink
Hello,

in general, the parent thread must exist first in order to create the child
thread. You are blocking the main-thread except polling nothing can happen
; it will sit there until it can be signaled, you don't have the necessary
code to handle this particular case, basically hijacking the main-loop is
always tricky. You must have a client worker thread.

--> main_loop != your network loop

https://en.wikipedia.org/wiki/Event_loop

Best.
Both client::handshakeCallback and server::handshakeCallback issue
async_read, but neither issues an async_write.
But if you take a look at the main method you can see I'm calling
c.send()
that will initiate a aync_write from client side. The cout in this method
is however never reached. > Why is it never called if it's not the
async_read that is 'blocking' the code?
Best regards,
Sandra
mm.w
2016-02-08 18:10:40 UTC
Permalink
Hello,

some conceptual views:

```
namespace
{
....asio::io_service srv;
....std::mutex mtx;
....std::condition_variable cond;
}

void notified()
{
....std::unique_lock<std::mutex> lck(mtx);
....cond.wait(lck); // we wait until signaled
....std::err << "I run then I post to me-self" << std::endl;
....srv.post(notified);
}

void notify()
{
....for(;;) { // infinite loop, should add control flow.
........std::this_thread::sleep(std::posix_time::seconds(1));
........std::lock_guard<std::mutex> lck(mtx);
........std::err << "you must run" << std::endl;
........cond.notify_all();
....}
}

int main(void)
{
....srv.post(notified);
....std::thread thr(notify);
....srv.run();
....// nothing will happen until srv stop.
....std::err << "exiting" << std::endl;
....thr.join();
....return 0;
}
```
Post by mm.w
Hello,
in general, the parent thread must exist first in order to create the
child thread. You are blocking the main-thread except polling nothing can
happen ; it will sit there until it can be signaled, you don't have the
necessary code to handle this particular case, basically hijacking the
main-loop is always tricky. You must have a client worker thread.
--> main_loop != your network loop
https://en.wikipedia.org/wiki/Event_loop
Best.
Both client::handshakeCallback and server::handshakeCallback issue
async_read, but neither issues an async_write.
But if you take a look at the main method you can see I'm calling
c.send()
that will initiate a aync_write from client side. The cout in this
method is however never reached. > Why is it never called if it's not the
async_read that is 'blocking' the code?
Best regards,
Sandra
Loading...