mutex

What is the correct way to create a single instance application?

Using C# and WPF under .net (rather than WindowsForms or console), what is the correct way to create an application that can only be run as a single instance? I know it has something to do with some mythical thing called a mutex, rarely can I find someone that bothers to stop and explain what one of these are. The code needs to also inf...

What is a mutex?

A mutex is a programming concept that is frequently used to solve multi-threading problems. My question to the community: What is a mutex and how do you use it? ...

Is this the proper use of a mutex?

I have a situation where I might have multiple instances of a program running at once, and it's important that just one specific function not be executing in more than one of these instances at once. Is this the proper way to use a mutex to prevent this from happening? lock (this.GetType()) { _log.Info("Doing Sync"); DoSync(); ...

Difference between binary semaphore and mutex.

Is there any difference between binary semaphore and mutex or they are essentialy same? ...

interacting with a CMutex without MFC

We have multiple MFC apps, which use CMutex( false, "blah" ), where "blah" allows the mutex to work across process boundaries. One of these apps was re-written without MFC (using Qt instead). How can I simulate the CMutex using Win32 calls? (Qt's QMutex is not inter-process.) I prefer not to modify the MFC apps. ...

Why do I need to SEM_PRIORITY_Q when using a VxWorks inversion safe mutex?

In VxWorks, I am creating a mutex with the SEM_INVERSION_SAFE option, to protect against the priority inversion problem. The manual says that I must also use the SEM_PRIORITY_Q option. Why is that? ...

C++ Thread, shared data

I have an application where 2 threads are running... Is there any certanty that when I change a global variable from one thread, the other will notice this change? I don't have any syncronization or Mutual exclusion system in place... but should this code work all the time (imagine a global bool named dataUpdated): Thread 1: while(1) ...

Intra-process coordination in mod_perl under the worker MPM

I need to do some simple timezone calculation in mod_perl. DateTime isn't an option. What I need to do is easily accomplished by setting $ENV{TZ} and using localtime and POSIX::mktime, but under a threaded MPM, I'd need to make sure only one thread at a time was mucking with the environment. (I'm not concerned about other uses of loca...

Are Mutexes needed in javascript?

I have seen this link: Implementing Mutual Exclusion in JavaScript. On the other hand, I have read that there are no threads in javascript, but what exactly does that mean? When events occur, where in the code can they interrupt? And if there are no threads in JS, do I need to use mutexes in JS or not? Specifically, I am wondering ab...

How can I implement java-like synchronization (monitors) using the Win32 API?

Each Java object (and its class) has an associated monitor. In pthread terms a Java monitor is equivalent to the combination of a reentrant mutex and a condition variable. For locking, the Win32 API provides Mutex objects (which are reentrant but heavyweight) and Critical Sections (which are non-reentrant but lightweight). It also provi...

PHP 5.x syncronized file access (no database)

Hi, I'm mostly familiar with Java, C and C++ in which there are ways to control that only one thread is accessing a resource at any given time. Now I'm in search for something similar but in PHP 5.x. To formulate my problem with one example: I have an ASCII-file which only stores a number, the value of a page load counter. At applicat...

Recursive Lock (Mutex) vs Non-Recursive Lock (Mutex)

POSIX allows mutexes to be recursive. That means the same thread can lock the same mutex twice and won't deadlock. Of course it also needs to unlock it twice, otherwise no other thread can obtain the mutex. Not all systems supporting pthreads also support recursive mutexes, but if they want to be POSIX conform, they have to. Other APIs ...

Why would WaitForSingleObject return WAIT_FAILED

MSDN says If the function fails, the return value is WAIT_FAILED. To get extended error information, call GetLastError. The code is: HANDLE m_mutex_handle; /**< m_mutex_handle. The handle to the created mutex. */ m_mutex_handle = ::CreateMutex( 0, false, NULL ); ::WaitForSingleObject( m_mutex_handle, INFINITE ); But what...

In a multi-threaded C++ app, do I need a mutex to protect a simple boolean?

I have a multi-threaded C++ app which does 3D rendering with the OpenSceneGraph library. I'm planning to kick off OSG's render loop as a separate thread using boost::threads, passing a data structure containing shared state in to the thread. I'm trying to avoid anything too heavyweight (like mutexes) for synchronization, as the render lo...

What is a good pattern for using a Global Mutex in C#?

The Mutex class is very misunderstood, and Global mutexes even more so. What is good, safe pattern to use when creating Global mutexes? ...

Synchronising twice on the same object?

I was wondering if in Java I would get any odd behaviour if I synchronise twice on the same object? The scenario is as follows pulbic class SillyClassName { object moo; ... public void method1(){ synchronized(moo) { .... method2(); .... } } public void me...

Do I need a semaphore when reading from a global structure?

A fairly basic question, but I don't see it asked anywhere. Let's say we have a global struct (in C) like so: struct foo { int written_frequently1; int read_only; int written_frequently2; }; It seems clear to me that if we have lots of threads reading and writing, we need a semaphore (or other lock) on the written_frequently me...

C#, return statement in a lock procedure, inside or outside?

I just realized that in some place in my code I have the return statement inside the lock and sometime outside. Which one is the best? 1) void example() { lock (mutex) { //... } return myData; } 2) void example() { lock (mutex) { //... return myData; } } Which one should I use? ...

What are the differences between various threading synchronization options in C#?

Can someone explain the difference between: lock (someobject) {} Using Mutex Using Semaphore Using Monitor Using Other .Net synchronization classes I just can't figure it out. It seems to me the first two are the same? ...

What is wrong with this tiny piece of mutex code?

// A Mutex allows threads mutually exclusive access to a resource. //----------------------------------------------------------------------- class Mutex { private: CRITICAL_SECTION m_mutex; public: Mutex() { InitializeCriticalSection(&m_mutex); } ~Mutex() { DeleteCriticalSection(&m_mutex); } void acquire() { Enter...