Solved

Message Queue processing without a  Mutex (C)

Posted on 2010-08-18
5
529 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

The case of the missing phone talks about the way a small electronic gadget (the mobile phone) has penetrated into our lives and has made us addicted to it.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

738 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