views:

95

answers:

2

Simple question, I think.

I have one thread that responds to a callback that is called when a user connects via TCP. That callback wants an answer if I accept or reject the login. Problem is I have to send a login request to a security server via asynchronous message passing and wait for a response.

What is the best way to handle this? Right now I have some code that just loops in the callback testing to see if the security server has sent a reply and when it comes in I read it and return the appropriate boolean. It just seems kind of gross.

TIA

A: 

Presumably, you would block on your asynchronous call to the security server to effectively make it synchronous.

Steven Sudit
Yeah, but block how, use a mutex, sleep and check a variable? The call to the SecSrv is asynch so once I call, it returns immediately and I have to do something explicit to block and wait for the return message. I'm really asking almost a style question I suppose, what's the smoothest way to code this?
shaz
I can't block in the asynchronous call, I send a message and receive a message back in a different thread some time later.
shaz
I was looking at the sample code here (http://www.windows-tech.info/13/e64a22df23c19164.php) and I didn't see any particular reason why your callback can't block on an Manual Reset Event before returning.
Steven Sudit
A: 

In the function that initiates the login check, after sending the message to request the check block on something. A Windows Event would work, as would a boolean flag and a boost::condition_variable or a boost::unique_future.

In the code that receives the response message from the security server set the event, or future or flag/condition variable. This will then wake up the initial function and allow it to return the appropriate response to the initial caller.

Anthony Williams