mutex

File.Exists() and File Access Synchronization across processes

I have a couple applications running in separate processes that each write to a central xml doc on the harddrive. I am using a named mutex to synchronize access to the file - but get the following exception often: System.IO.IOException: The requested operation cannot be performed on a file with a user-mapped section open. I realized ...

Mutex Mechanisms

What is most apropriate mutex alg in C#/.NET for this kind of task. many reads few incremental changes (up to 3 in "go forward" state machine ) very low collision probability (does collision probability matter?). I was thinking about simple lock or ReaderWriterLockSlim , but I am not sure which one to choose and if there is somethi...

Mutual Exclusion and Semaphores

Hello. I am writing a program (for homework) that simulates a unisex bathroom. Only 4 people are allowed at a time and men and woman cannot enter if the other sex is already using the bathroom. My problem is with allowing a max of 4 people in the bathroom. As you can see from the output, only 1 person is getting into the restroom at ...

concurrent threads in C programming

I have encountered a problem while implementing wait and signal conditions on multiple threads. A thread needs to lock a mutex and wait on a condition variable until some other thread signals it. In the meanwhile, another thread locks the same mutex and waits on the same condition variable. Now, the thread which is running concurrently ...

Should I put KeepAlive inside my finally block?

So this tells me that I should put a GC.KeepAlive at the end of my code to keep my mutex open (to prevent multiple instances of my app happening due to early GC disposal of my mutex). But should I put the KeepAlive in my finally block or at the end of my try block? ...

Boost Condition Variable Argument Error

Hello to all, i encounter an error in the code below. recursive_mutex m_RecurMutex; condition_variable cond; unique_lock<recursive_mutex> lock(m_RecurMutex); cond.wait(lock); // Error Here. What is the reason cause this error ? Thanks. ...

How can I wait for a signal before proceeding compilation of other code segment

Can I wait for a signal from a event so that , when I recievce the signal then only I will proceed with next code segment. For making it clear , I have the follwoing code: hiddenMediaElement.Source = new Uri(strMediaFileName, UriKind.RelativeOrAbsolute); hiddenMediaElement.MediaFailed += (obj, Sender) => ...

How can I synchronize three threads?

My app consist of the main-process and two threads, all running concurrently and making use of three fifo-queues: The fifo-q's are Qmain, Q1 and Q2. Internally the queues each use a counter that is incremented when an item is put into the queue, and decremented when an item is 'get'ed from the queue. The processing involve two threads,...

C Pthreads mutex values?

I am writing a program with a few critical sections. The thing is I need to check the value of a mutex in an if statement. I would like to do something like this: if pthread_mutex(&mutex) == 0 // locked // Do something else if pthread_mutex(&mutex) == 1 // unlocked // Do something else Is this possible? ...

Are there any platforms that do not support reentrent mutex's or recursive locks?

I'm wondering if my implementation should expect that reentrant mutex's are supported or not. The code is supposed to portable/platform independent. I'm wondering if mutex recursion is common enough that it shouldn't be a concern. ...

Mutex lock: what does "blocking" mean?

I've been reading up on multithreading and shared resources access and one of the many (for me) new concepts is the mutex lock. What I can't seem to find out is what is actually happening to the thread that finds a "critical section" is locked. It says in many places that the thread gets "blocked", but what does that mean? Is it suspende...

Updating global variables from a single worker thread: Do I need mutexes?

It seems that this question gets asked frequently, but I am not coming to any definitive conclusion. I need a little help on determining whether or not I should (or must!) implement locking code when accessing/modifying global variables when I have: global variables defined at file scope a single "worker" thread reading/writing to glob...

How to allow only one user to run my C# program, but also allow multiple instances?

The situation is that I want to allow users to open multiple instances of the program but I do not want more than one logged on user to use the program at once. So, for instance if the program is installed on a server and multiple users can Remote Desktop to the server I only want one user to be able to run the program but still be able ...

Producer-Consumer model - binary semaphore or mutex??

This is mainly about the understanding of the concept, which confuses me. Mutex means that one thread takes the control of the access of shared resource, performs operations and unlocks it, then only other thread can gain access to lock while binary semaphore is like a thread can gain access to the shared resource but gaining acces...

Possible to define a function-like macro with a variable body?

I've been looking at the GCC docs for defining macros and it looks like what I want isn't possible, but I figure if it is, someone here would know. What I want to do is define this macro: synchronized(x) { do_thing(); } Which expands to: { pthread_mutex_lock(&x); do_thing(); pthread_mutex_unlock(&x); } In C++ I could...

When should we use mutex and when should we use semaphore

When should we use mutex and when should we use semaphore ? ...

Multithreading: classical Producer Consumer algorithm

Hi all, Something I don't get about the classical algorithm for the Producer-Consumer problem (from Wikipedia:) semaphore mutex = 1 semaphore fillCount = 0 semaphore emptyCount = BUFFER_SIZE procedure producer() { while (true) { item = produceItem() down(emptyCount) down(mutex) putItemIn...