mutex

Run single instance of an application using Mutex

In order to allow only a single instance of an application running I'm using mutex. The code is given below. Is this the right way to do it? Are there any flaws in the code? How to show the already running application when user tries to open the application the second time. At present (in the code below), I'm just displaying a message t...

What is the reasoning behind the double call to WeakHashMap.put( .. ) ?

This blog post demonstrates a way to implement a mutex per string id idiom. The String ids used are to represent HttpSession ids. Why do we need to wrap a WeakReference around the Mutex instances ? Isn't it better to just create a Map from String -> Mutex ? Why do we need to call put twice ? public Mutex getMutex( String id ) { ...

Non threadlocked Mutex

This is an offshoot of this question but with a few constraints removed. I have a system where I need to manage file locking. I need to be able to lock a file (shared read locking) in one thread and then unlock it in another. More accurately, I can't be sure what thread it will be unlocked in or even if the creating thread is still arou...

Using Named Mutex

I have two instances running of same Windows Service. They check the health of each other and report if any issue is found. I have a critical job that needs to be performed so I am running it with a fail-over approach, it runs in Master, and if Master is not responding it runs in slave. This job needs to communicate over a specific seria...

Do I need a lock when only a single thread writes to a shared variable?

I have 2 threads and a shared float global. One thread only writes to the variable while the other only reads from it, do I need to lock access to this variable? In other words: volatile float x; void reader_thread() { while (1) { // Grab mutex here? float local_x = x; // Release mutex? do_stuff_wi...

Plural form of word "mutex"

What is the correct plural form of the portmanteau mutex. Is it mutexes or mutices? ...

Odd/Incorrect Semaphore Behavior on OS X

Hi, I have some very basic semaphore code that works great on Linux, but cannot for the life of me get it to run properly on OS X... It returns the oddest of results... #include <iostream> #include <fcntl.h> #include <stdio.h> #include <semaphore.h> int main() { sem_t* test; test = sem_open("test", O_CREAT, 0, 1); int val...

Is there a difference between Boost's scoped mutex and WinAPi's critical section?

The title says it all. In Windows environment, is Boost's scoped mutex using WinAPI's critical sections, or something else? ...

destructor of static class need a mutex?

We have a static (singleton) class which will be used in a mutithreaded environment. We use mutex in its constructor and other mrmber functions. However there is no mutex for the destructor. Destructor do some tasks like cleaning up of some other member objetcs etc. Do we need to a mutex in the distructor also ? ...

segmentation fault on pthread_mutex_lock

hey, sorry to be a bother, but I need help urgently. I searched for this before I posted this question. I'm getting a segmentation fault when I try to do pthread_mutex_lock(&_mutex). This is really odd, I'm not sure what might have caused it. I have initialized _mutex in the constructor with pthread_mutex_init(&_mutex,NULL). anythi...

.NET: Using Mutex to make sure a webservice is only called once at a time

I'm using a Mutex to make sure a webservice is only running once at a time, but I can't get it 100% right with WaitOnce and ReleaseMutex. I've got this: private static Mutex mutex = new Mutex(); [WebMethod] public bool TriggerAll() { bool ranJobs = false; try { if (mutex.WaitOne(0, f...

Reliably detecting that another of my applications is running

I have two applications, a WinForms app and a Windows Service that will both run on the same machine. I want the WinForms app to reliably detect when the service is running. I have complete control over the design and implementation of both applications. My first thought is to use a Mutex, instantiated by the Service and detectable by ...

Sleep while holding a boost::interprocess::scoped_lock causes it to be never released

Hi, I'm doing IPC on Linux using boost::interprocess::shared_memory_object as per the reference (anonymous mutex example). There's a server process, which creates the shared_memory_object and writes to it, while holding an interprocess_mutex wrapped in a scoped_lock; and a client process which prints whatever the other one has written ...

Example for boost shared_mutex (multiple reads/one write)?

I have a multithreaded app that has to read some data often, and occasionally that data is updated. Right now a mutex keeps access to that data safe, but it's expensive because I would like multiple threads to be able to read simultaneously, and only lock them out when an update is needed (the updating thread could wait for the other thr...

C++/GLFW - The right way to use Mutex objects ?

Hi, I'm working on a simulation that uses multithreading extensively. The thing is that, until now i've never used any mutex objects to protect my data. And the result, is that i'm getting bunch of segmentation faults.. I'm trying to lock/unlock with mutex while : reading/writing but that causes me another segfault : #0 77D27DD2 ntdl...

Not locking mutex for pthread_cond_timedwait and pthread_cond_signal ( on Linux )

Is there any downside to calling pthread_cond_timedwait without taking a lock on the associated mutex first, and also not taking a mutex lock when calling pthread_cond_signal ? In my case there is really no condition to check, I want a behavior very similar to Java wait(long) and notify(). According to the documentation, there can be ...

How to avoid multiple application instances?

I have an application, "myprogram.exe", which calls functions and code inside a dll, one of this functions that "myprogram.exe" calls create a new instance of a winform, "MyForm.cs" and then show it using form.show();. I can have 'n' number of "myprogram.exe" instances running, but I want to have only one instance of "MyForm.cs" for eac...

Mutex in Python Twisted

I'm using the Twisted framework, and am getting RPCs asynchronously. I have another function which does a task every 2 seconds, and sleeps in between. This is called through reactor.callInThread. These depend on a shared resources, so I need some thread-safe way of accessing them. How does one go about using critical sections / mutexes /...

Differences between Conditional variables, Mutexes and Locks

For example the c++0x interfaces I am having a hard time figuring out when to use which of these things (cv, mutex and lock). Can anyone please explain or point to a resource? thanks in regard ...

Ideal way to single-instance apps on the Mac

On Windows, it's common practice to create a named mutex and use the presence of that to determine that an instance of a given app is already running. This has its drawbacks, but mostly works. I can think of a ways to do this on the Mac: named pthread mutexes enumerate running processes and look for one that matches create and lock a ...