mutex

How are mutex and lock structures implemented?

I understand the concept of locks, mutex and other synchronization structures, but how are they implemented? Are they provided by the OS, or are these structures dependent on special CPU instructions for the CPUs MMU? ...

Bring window to foreground after Mutex fails

Hi, i was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. Eg - Application X is running but is in the background. I cant see it so i try to run another instance and because the mutex returns false the best thing to do is to br...

Question about mutexes and locks

Are the two code samples below equivalent? Poco::ProcessHandle::PID ProcessRunner::processId() const { Poco::ProcessHandle::PID pid = 0; mMutex.lock(); pid = mPID; mMutex.unlock(); return pid; } , Poco::ProcessHandle::PID ProcessRunner::processId() const { Poco::ScopedLock<Poco::Mutex> lock(mMutex); return...

Explain the Need for Mutexes in Locales, Please

Reading the question Why doesn’t C++ STL support atoi(const string& ) like functions?, I encountered a comment which warned that GCC (at least) has a bug that can slow down multi-threaded applications which use ostringstream frequently. This is apparently due to a mutex 'needed' by the C++ locale machinery. Given my recent interest in g...

If you unlock an already unlocked mutex, is the behavior undefined?

If you unlock an already unlocked mutex, is the behavior unsafe, safe, or undefined? The purpose of the question is related to the following code, where I don't know if it would be better to unlock the mutexes within the if block, or just outside the if block. // This chunk of code makes dual locking semi-autonomous. int c_lckd...

rails - implementing a simple lock to prevent user's from editing the same data concurrently

Hi - I have an app where I need to prevent users from editing data while it is being edited by a different user. I'm trying to think of the best way to do it and wanted to ask for ideas. So far, I've created a settings model that stores application wide configuration on the db in key/value pairs. So, for the lock, I have a settings insta...

Locks and Mutexes in C++

Hi everyone, I learn C++ for a while and still didn't come across good book which would explain what are those beasts? Are they integral C++ feature? If so how is it that they are only mentioned in such book like The C++ Programming Language by B.S. If not, where can you get reliable information about them - prefferably a book (don't rea...

Providing multiple instances of a form yet processing events one at a time

I need to be able to let multiple instances of the same form be open as my application can be used in different places at once. On the other hand i need to be able to process the operations during the "OK" event one at a time to ensure data is stored safely and not overwritten by another form instance by accident. I show my form using t...

Restrict certain URLs to a single thread

I have a nice little threading problem with a library I use to generate and serve dynamic graphs and charts in Zope. See this question for a description of my original problem. As the website is already in production, I don't have time to debug that library (I'm not an expert in either C nor threading), hence I'm looking for the quick f...

Is there a UNIX/pthreads equivalent to Windows manual reset events?

Briefly, a manual reset event is a synchronization construct which is either in the "signaled" or "nonsignaled" state. In the signaled state, any thread which calls a wait function on the event will not block and execution will continue unaffected. Any and all threads which calls a wait function on a nonsignaled object will block until...

Checking Mutex release

I have a multithreaded application written in C++. And I'm using mutex for file writes. I have a suspicion that somewhere during the execution of the program, the mutex isn't being released. So I was wondering if there was a way to check for mutex locks and releases on a file, programmatically or otherwise. I'm running the code on SuseLi...

Threads and simple Dead lock cure

When dealing with threads (specifically in C++) using mutex locks and semaphores is there a simple rule of thumb to avoid Dead Locks and have nice clean Synchronization? ...

Why is this program in error? `Object synchronization method was called from an unsynchronized block of code`

What is wrong with this code? i get a 'Object synchronization method was called from an unsynchronized block of code'. I found one result on google that said i may be releasing a mutex before locking but according to my output this is not the case. Here is the mutex code without the other code in between. -edit- sorry guys, wrong paste....

How do i make the mutex not be recusive?

I ran the code below expecting flow to be locked on the 2nd time i lock a mutex. After running it twice i realize it can lock many times (assuming in the same thread) without stopping. How do i change this behavior? using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace Test { class Pr...

Mutex in JNI using Foundation NSLock

I have some objective-c code that uses an NSLock to implement a sort of transaction. The object is locked on a "begin transaction", several other calls are made with the lock in place, and then it's released with a "commit". I'm writing a JNI glue layer to access this code from Java, but the lock is behaving differently in JNI vs pure ...

How to call win32 CreateMutex from .Net

Hello, I've been successfully creating a .net mutex like this: SingleIns = new Mutex(true, AppName); for a while. It works in XP, Vista, but apparently not in Windows7. So I need to make an interop call to a Win32 library so other Com components can identify the mutex. I found the following code, but the Win32Calls. is not found... is...

boost::mutex / How to test if a mutex is locked

I know, I know, the title of my message may seem provocative, since boost::mutex purposefuly do not expose lock / unlock (in order to avoid dead locks). However the boost documentation is quite short on these aspects (to say the least), so I am asking if anyone can help me in the following use case. Suppose you have a class Foo, which...

Are spinlocks a good choice for a memory allocator?

I've suggested to the maintainers of the D programming language runtime a few times that the memory allocator/garbage collector should use spinlocks instead of regular OS critical sections. This hasn't really caught on. Here are the reasons I think spinlocks would be better: At least in synthetic benchmarks that I did, it's several t...

Critical Sections that Spin on Posix?

The Windows API provides critical sections in which a waiting thread will spin a limited amount of times before context switching, but only on a multiprocessor system. These are implemented using InitializeCriticalSectionAndSpinCount. (See http://msdn.microsoft.com/en-us/library/ms682530.aspx.) This is efficient when you have a critic...

Win32: How to get the process/thread that owns a mutex?

I'm working an application of which only one instance must exist at any given time. There are several possibilities to accomplish this: Check running processes for one matching our EXE's name (unreliable) Find the main window (unreliable, and I don't always have a main window) Create a mutex with a unique name (GUID) The mutex option...