Click here to Skip to main content
16,005,121 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Japanese Support for MFC Pin
Programm3r7-Mar-07 21:39
Programm3r7-Mar-07 21:39 
GeneralRe: Japanese Support for MFC Pin
micutzu7-Mar-07 21:49
micutzu7-Mar-07 21:49 
GeneralRe: Japanese Support for MFC Pin
Programm3r7-Mar-07 21:50
Programm3r7-Mar-07 21:50 
AnswerRe: Japanese Support for MFC Pin
Hamid_RT7-Mar-07 23:41
Hamid_RT7-Mar-07 23:41 
QuestionMonitoring a Process [modified] Pin
Programm3r7-Mar-07 21:10
Programm3r7-Mar-07 21:10 
AnswerRe: Monitoring a Process Pin
Programm3r7-Mar-07 23:42
Programm3r7-Mar-07 23:42 
QuestionMultithreading issue Pin
Cedric Moonen7-Mar-07 21:05
Cedric Moonen7-Mar-07 21:05 
AnswerRe: Multithreading issue Pin
Roger Stoltz7-Mar-07 22:27
Roger Stoltz7-Mar-07 22:27 
This is a nice little communication related problem. Big Grin | :-D

I've come across this problem a few times and I've solved it in a similar way each time.
The best suited solution might depend on the protocol you're using, if it's possible to assign an ID to each message that can be used to map the answer to the request.
I assume this is your case.
It also depends on whether the protocol is asynchronous or not, i.e. if you are able to send multiple requests before you get an answer for the first request.
I assume the protocol is synchronous. Correct me if I'm wrong Cédric.

I'm not sure if I can describe how I've done this without a whiteboard, but I'll try...;)
Usually I have a sending thread that reads from a queue that contains reference counting smart pointers to the messages to be sent. All queues mentioned later contains reference counting smart pointers to message objects, even if i refer to them as "messages".

When a message has been sent that requires a reply I put the message into another queue which is read by a thread (the timeout thread) that handles the timeout, but also waits for incoming messages on a semaphore.
A thread that takes care of the message receiving mechanism puts the received messages into the queue read by the timeout thread and releases the semaphore. This will release the timeout thread which is wating on a reply to the front message from the timeout queue and if the message tags/IDs match I've got a reply for the request sent earlier. The other situation is that the timeout expires in the timeout thread which returns from ::WaitForMultipleObjects(...) with the index of the waitable timer that handles the timeout. Perhaps the request message should be re-sent and put into the sending queue again depending on the protocol used.

If the protocol is asynchronous the timeout thread should perhaps have a local container, e.g. a vector, with messages that can be searched for message IDs when a new message arrives. Messages sent should be put at the end of the vector and the waiting is done on the message at the beginning of the vector.

By using this threading and queueing technique you don't risk losing replies if the are immediate, you can even put a message in the timeout queue before sending it.

Each thread is waiting on synchronization objects such as semaphores and waitable timers.
If you have a look at Joe Newcomer's article about semaphores here[^] you should get an idea of how I release the different threads using semaphores when there are new objects in their queues.

N.B. The above requires an integer member in the CMessage class that contains the timeout value in preferably millisecs. The timeout thread creates a waitable timer.

I hope you get what I'm getting at, but let me know if you don't. Smile | :)

--
Roger


"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown

GeneralRe: Multithreading issue Pin
Cedric Moonen7-Mar-07 23:14
Cedric Moonen7-Mar-07 23:14 
GeneralRe: Multithreading issue Pin
Roger Stoltz8-Mar-07 1:50
Roger Stoltz8-Mar-07 1:50 
GeneralRe: Multithreading issue Pin
Cedric Moonen8-Mar-07 2:29
Cedric Moonen8-Mar-07 2:29 
GeneralRe: Multithreading issue Pin
Roger Stoltz8-Mar-07 3:03
Roger Stoltz8-Mar-07 3:03 
GeneralRe: Multithreading issue Pin
Cedric Moonen8-Mar-07 3:35
Cedric Moonen8-Mar-07 3:35 
QuestionInstallation through MFC code Pin
jeepoo7-Mar-07 20:23
jeepoo7-Mar-07 20:23 
AnswerRe: Installation through MFC code Pin
CPallini7-Mar-07 20:55
mveCPallini7-Mar-07 20:55 
GeneralRe: Installation through MFC code Pin
micutzu7-Mar-07 21:33
micutzu7-Mar-07 21:33 
GeneralRe: Installation through MFC code Pin
CPallini8-Mar-07 0:52
mveCPallini8-Mar-07 0:52 
GeneralRe: Installation through MFC code Pin
micutzu8-Mar-07 1:01
micutzu8-Mar-07 1:01 
Questiondsd Pin
anilprincy7-Mar-07 20:06
anilprincy7-Mar-07 20:06 
AnswerRe: dsd Pin
Rajesh R Subramanian7-Mar-07 20:10
professionalRajesh R Subramanian7-Mar-07 20:10 
GeneralRe: dsd Pin
toxcct7-Mar-07 21:26
toxcct7-Mar-07 21:26 
GeneralRe: dsd Pin
Rajesh R Subramanian7-Mar-07 22:39
professionalRajesh R Subramanian7-Mar-07 22:39 
GeneralRe: dsd Pin
Programm3r7-Mar-07 21:29
Programm3r7-Mar-07 21:29 
GeneralRe: dsd Pin
Roger Stoltz7-Mar-07 22:49
Roger Stoltz7-Mar-07 22:49 
GeneralRe: dsd Pin
Rajesh R Subramanian7-Mar-07 22:57
professionalRajesh R Subramanian7-Mar-07 22:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.