Solved

Simple message queue for Windows C application

Posted on 2010-08-15
15
696 Views
Last Modified: 2012-05-10
Hello,

I'm hoping someone can provide a reference or example.

I have a multi-threaded application in C.  I'd like to use message queues to communicate between two of the threads and be able to pass messages of ~16 bytes.  I'm developing this under Windows right now but will port this to a different embedded operating system in the future.  I realize the thread and message queue functions will be different under a different operating system.

What I'm looking for is the simple way to do a message queue under Windows.  The MSDN literature immediately starts talking about GUI windows and system messages.  I'm not interested in either.

All I want to do is create the queue, pend a thread listening on the queue, and be able to write to it.

Any pointers or examples would be appreciated.

Thanks.
0
Comment
Question by:JohnSantaFe
  • 4
  • 4
  • 2
  • +4
15 Comments
 
LVL 7

Expert Comment

by:jhp333
ID: 33443091
I think you're talking about message queues for IPC (Inter-Process Communication).
According to this page, http://msdn.microsoft.com/en-us/library/aa365574%28VS.85%29.aspx
Windows does not provide messages queues as a way of IPC.

You can try with user defined Windows messages, which are mainly for GUI by the way, but it's quite different from IPC message queue.
0
 
LVL 33

Assisted Solution

by:pgnatyuk
pgnatyuk earned 50 total points
ID: 33443129
>> I'm developing this under Windows right now but will port this to a different embedded operating system in the future

In case you want to send a message to the thread, you can do it with PostThreadMessage:
http://msdn.microsoft.com/en-us/library/ms644946(VS.85).aspx

For example :
PostThreadMessage(nThreadId, WM_MY_MESSAGE, wData, lData);

I'm sure, you can simply put these 16 bytes somewhere in a global variable protected with the critical section and call SetEvent in order to notify the thread about the new data coming.

About thread synchronization:
CodeProject. Thread Synchronization for Beginners
http://www.codeproject.com/KB/threads/Synchronization.aspx

0
 
LVL 11

Expert Comment

by:cup
ID: 33443397
You could just use an STL queue if it is between threads.  Just do a mutex lock when performing the push and pop.

Since it is threads, they share the same address space so anything global will work.
0
 
LVL 5

Expert Comment

by:xdomains
ID: 33444358
If you are looking for a platform independent messaging library in C++, here is it -
http://www.sixtyfourbit.org/mq4cpp.htm

There are others as well, so depending on your requirement, need to select the right one.
0
 

Author Comment

by:JohnSantaFe
ID: 33447938
Hmmm.  I've used four other operating system that all had an inter-thread message queue as a fundamental component which other threads pend and wait on.  I'm surprised how difficult this seems under windows.

PostThreadMessage is close to what I'm use to, however, my receiving thread can't pend and wait for the message.

Maybe I'm not thinking of this in Windows terms.  My design problem is I have one thread listening to an ethernet port.  I have a second thread responding to commands that came in on the ethernet port and sending data out a slow serial link.  Because the serial link is slow, I don't want to tie up a single thread and not listen to the ethernet port for too long.  It is possible (and even likely) that while I'm sending data out the serial port additional commands will come in the ethernet port.

I could use an event, however if the serial link is busy and/or multiple events come in, the serial thread will not know it.  A counting semaphore might be possible to keep track that there are more things for the serial link thread to perform...? A queue of some sort is needed so the ethernet listening thread can load it while the other is busy.

Any other comments or suggestions would be appreciated.

Thanks.
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33448016
1. Implement this queue yourself.
2. Try to use boost. It's a kind of a fashion now, but seems like this is exactly what you need.

BTW,
MSDN. Message Queuing (MSMQ)
http://msdn.microsoft.com/en-us/library/ms711472(VS.85).aspx
0
 
LVL 86

Accepted Solution

by:
jkr earned 400 total points
ID: 33448086
I'd recommend thinking about using 'PostThreadMessage()' together 'GlobalAlloc()'. This way, the data chinks will be placed in the receiving thread's message queue where you can process them one after one. I.e.
#define WM_DATAREADY (WM_USER + 0)



// thread on ethernet port



LPVOID pv = GlobalAlloc(GPTR,BUF_SIZE);



recv(s,(char*)pv,BUF_SIZE,0);



PostThreadMessage(nSerialThread,WM_DATAREADY,0,(LPARAM)pv);





// thread on serial port



MSG msg;



while(GetMessage(&msg,NULL,0,0)) {



  if(WM_DATAREADY == msg.message) {



    LPVOID pv = (LPVOID) msg.lParam;



    SendToSerialPort(pv);



    GlobalFree(pv);

  }

}

Open in new window

0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:JohnSantaFe
ID: 33448323
JKR,

Will the thread suspend when using GetMessage()  ?
If not, would using WaitMessage() followed by GetMessage() make sense so the thread isn't constantly spinning?
0
 
LVL 86

Expert Comment

by:jkr
ID: 33448382
The thread will be suspended, as 'GetMessage()' is a blocking call and will not return until at least one message is received, see http://msdn.microsoft.com/en-us/library/ms644936%28VS.85%29.aspx ("GetMessage")
0
 

Author Comment

by:JohnSantaFe
ID: 33448489
Thanks the wording in the MSDN article was a little unclear to me.
I'll give this a try and post back later.
0
 
LVL 22

Expert Comment

by:ambience
ID: 33449269
Check POCO library, it has pretty decent NotificationQueu and NotificationCenter classes that will do exactly what you want to in a portable manner.
0
 
LVL 22

Expert Comment

by:ambience
ID: 33449278
Oh and it also has SocketReactor pattern implemented so that would be another useful goodie.
0
 
LVL 22

Assisted Solution

by:ambience
ambience earned 50 total points
ID: 33449288
0
 
LVL 22

Expert Comment

by:ambience
ID: 33449313
Sorry for multiple comments, but im a dizzy at the moment.
http://pocoproject.org/docs/Poco.NotificationCenter.html 
0
 

Author Closing Comment

by:JohnSantaFe
ID: 33458101
I decided to go with JKR's suggestion of using PostThreadMessage and GetMessage.  

The libraries, especially Poco look pretty interesting, but for now I just want something simple.

Thanks for the help.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
computer science syllabus 3 70
How can i compile this github project?? 2 74
Computer software inventory 5 73
Acrinis True image 2 54
Can I legally transfer my OEM version of Windows to another PC?  (AKA - Can I put a new systemboard in my OEM PC?) Few of us are both IT and legal experts but we all have our own views of Microsoft's licensing rules and how they apply.  There are…
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now