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

C / C++ / MFC

 
QuestionJapanese Support for MFC Pin
micutzu7-Mar-07 21:30
micutzu7-Mar-07 21:30 
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 
Hello,

I am developping a multithreaded application and I came across a quite complex problem. In brief, suppose that the application can send 'messages' over a communication link (which is pluggable).
In a particular situation, I would like to send a message and wait until I receive a answer (with a timeout). Here is how the CMessage class looks like 9only relevant parts):

CMessage
{
public:
   void send();
   CMessage* getReplyBlocking(unsigned long timeout);

   void setReply(CMessage* pReply);

protected:
   HANDLE m_hReplyEvent;
   CMessage* m_pReply;
};


So, little explanation: to send the message, we call the send function. This function sends the message over a certain communication link (and we don't really care about how it is implemented). Then, we can call getReplyBlocking to wait for an answer of this message. The communication link, when data is received back, will call the setReply function providing the reply.

Here is the implementation of getReplyBlocking:
CMessage* CMessage::getReplyBlocking(unsigned long timeout)
{
   m_hReplyEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
   WaitForSingleObject(m_hReplyEvent, timeout);

   return m_pReply;
}


Here is the implementation of setReply:
void CMessage::setReply(CMessage* pReply)
{
   m_pReply = pReply;
   if (m_hReplyEvent != NULL)
       SetEvent(m_hReplyEvent );
}



But this design has a some major flaws:
1) If the reply to the message is almost immediate (because for example we plug a test communication link), the setReply function will be called before getReplyBlocking is even called. So, problem: I will wait until I have a timeout and in fact I have already me reply. I could solve partially this issue by first testing if I haven't an answer already (but then, the answer could still be sent between the check and the start of the WaitForSingleObject).
2) To make things more complex, the assignement of the reply may be non-atomic. In fact, to simplify the example I used a simple pointer but in reality, it is a smart pointer. It means that if I have a reply just at the time that my WaitForSingleObject times out, then I am in big trouble (I will start assigning my smart pointer and I will finally return a smart pointer which is not fully constructed).

It must exist mechanisms to solve these kind of problems but I don't really see how I can solve this.
BTW, I am perfectly aware of Semaphores, Critical sections, ... but I failed to see how they can solve these problems (and without introducing new ones).

If somebody has an idea, it will be highly appreciated Wink | ;)
Thanks


Cédric Moonen
Software developer

Charting control [v1.1]

AnswerRe: Multithreading issue Pin
Roger Stoltz7-Mar-07 22:27
Roger Stoltz7-Mar-07 22:27 
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 

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.