Solved

Message Queue processing without a  Mutex (C)

Posted on 2010-08-18
5
518 Views
Last Modified: 2012-05-10
Hi,

I'm trying to dream up a clever way to create a Message Queue processing system with multiple threads without the use of a mutex for maximum performance. Since I'm working in C, the Message Queue could be a linked pointer list. Multiple threads would be adding to the Message Queue (adding to the linked pointer list). Only one thread would be processing Messages and removing them from the queue in FIFO order.

Guess the problem I see is if an input and the processor thread both try to modify the first message in the list. Is there a good way to do this without a mutex or is a mutex the way to go?
0
Comment
Question by:php-newbie
  • 3
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 33465721
>>Is there a good way to do this without a mutex or is a mutex the way to go?

No. That's what mutexes were created for. There are some workaround techniques like using a variable as a lock, but I would not recommend using that.

BTW, if you were on Windows, one way to reduce the performance footprint would be to use a CRITICAL_SECTION (http://msdn.microsoft.com/en-us/library/ms682530(VS.85).aspx) instead of a Mutex object.
0
 
LVL 8

Expert Comment

by:Anthony2000
ID: 33466092
Curious, jkr, if on Windows, would you not use the MessageQueue api? (I am not sure if it is thread safe?). Unless this is an exercise to create your own?
0
 
LVL 86

Expert Comment

by:jkr
ID: 33466239
Sure, but that API is to manage a whole different beast - IMO that would be overkill for a simple message queue...
0
 

Author Comment

by:php-newbie
ID: 33466345
Ahhh, we're on Win, Linux, Android and I need a common implementation across all platforms. Thanks!
0
 
LVL 86

Expert Comment

by:jkr
ID: 33466431
For a common implementation, check out the 'pthreads' library, it's available on almost all platforms and of course has mutexes.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
A short article about problems I had with the new location API and permissions in Marshmallow
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.

914 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

16 Experts available now in Live!

Get 1:1 Help Now