mutex

Should mutexes be used to ensure concurrency, while using POSIX message queues?

I am using a single POSIX message queue in my application which is accessed by multiple readers. Should I use mutexes in this scenario? ...

How best to test a Mutex implementation?

What is the best way to test an implementation of a mutex is indeed correct? (It is necessary to implement a mutex, reuse is not a viable option) The best I have come up with is to have many (N) concurrent threads iteratively attempting to access the protected region (I) times, which has a side effect (e.g. update to a global) so that t...

Do pthread Mutexs work across threads if in shared memory?

I found this: http://stackoverflow.com/questions/2284730/fast-interprocess-synchronization-method I used to believe that a pThread mutex can only be shared between two thraeds in the same address space. The question / answers there seems to imply: If I have two separate proceses A & B. They ahve a shared memory region M. I can put a p...

When do c++ stream objects use mutexes?

In the answer to this question ovanes states: Please be aware that boost::lexical_cast is much slower as atoi. I also use it very often in a performance non-critical code. The problem with lexical_cast is that it uses stringstream for conversion. If you are working in a multi-threaded environement any stream class from ...

When to use recursive mutex?

I understand recursive mutex allows mutex to be locked more than once without getting to a deadlock and should be unlocked the same number of times. But in what specific situations do you need to use a recursive mutex? I'm looking for design/code-level situations. ...

Is it a good idea to use the existence of a named mutex as an indicator?

I'm using a named mutex to detect other instances of my application and exit accordingly, and found that there are two ways of doing this: Create the mutex; ignore the indication whether it already existed; try to acquire it; use the fact that acquire succeeded/failed. Create the mutex; use the indication whether it already existed. ...

ASP.NET How to process only 1 HTTP handler at a time from a specified user?

Hi. I have the next problem: I need to process only 1 request at a time from each user. Lets assume that server identifies each user be UserID, sent in query string. How can make the server work the way like FIFO (do not start processing next request until the previous is fully processed)? Should I use the named mutexes inside HTTP handl...

What are some good ways to do intermachine locking?

Our server cluster consists of 20 machines, each with 10 pids of 5 threads. We'd like some way to prevent any two threads, in any pid, on any machine, from modifying the same object at the same time. Our code's written in Python and runs on Linux, if that helps narrow things down. Also, it's a pretty rare case that two such threads wan...

How do you protect a common resource using mutexes?

I have a common resource, which I want 1 and only 1 instance of my application (or it's COM API) to have access to at any time. I have tried to protect this resource using mutexes, but when multiple threads of a host dotnet application try to access the COM object, the mutex doesn't seem to be released. This is the code I have used to pr...

Thread resource sharing

I'm struggling with multi-threaded programming... I have an application that talks to an external device via a CAN to USB module. I've got the application talking on the CAN bus just fine, but there is a requirement for the application to transmit a "heartbeat" message every second. This sounds like a perfect time to use threads, so I ...

Do condition variables still need a mutex if you're changing the checked value atomically?

Here is the typical way to use a condition variable: // The reader(s) lock(some_mutex); if(protected_by_mutex_var != desired_value) some_condition.wait(some_mutex); unlock(some_mutex); // The writer lock(some_mutex); protected_by_mutex_var = desired_value; unlock(some_mutex); some_condition.notify_all(); But if protected_by_mutex...

Problem with semaphores and sem_wait()

I have a queue structure that is being used by several pthreads. The threads are supposed to dequeue from the queue if it's not empty and then do their business. I initially had this set up as a while loop where the threads checked whether the queue was empty using a mutex_lock. Unfortunately this slowed my program down to a crawl. I t...

Mutex in C# for the same assembly

We have an assembly that is called repeatedly by different executables and it shares these states how its like not thread safe because they call the code at same time what kinda mutex to use ...

Thread Synchronisation 101

Previously I've written some very simple multithreaded code, and I've always been aware that at any time there could be a context switch right in the middle of what I'm doing, so I've always guarded access the shared variables through a CCriticalSection class that enters the critical section on construction and leaves it on destruction. ...

Why boost::recursive_mutex is not working as expected?

I have a custom class that uses boost mutexes and locks like this (only relevant parts): template<class T> class FFTBuf { public: FFTBuf(); [...] void lock(); void unlock(); private: T *_dst; int _siglen; int _processed_sums; int _expected_sums; int _assign...

How can I synchronize database access between a write-thread and a read-thread?

My program has two threads: Main execution thread that handles user input and queues up database writes A utility thread that wakes up every second and flushes the writes to the database Inside the main thread, I occasionally need to make reads on the database. When this happens, performance is not important, but correctness is. (I...

Implementing a Mutex Lock in C

I'm trying to make a really simple spinlock mutex in C and for some reason I'm getting cases where two threads are getting the lock at the same time, which shouldn't be possible. It's running on a multiprocessor system which may be why there's a problem. Any ideas why it's not working? void mutexLock(mutex_t *mutexlock, pid_t owner) { i...

Fast inter-process (inter-threaded) communications IPC on large multi-cpu system.

What would be the fastest portable bi-directional communication mechanism for inter-process communication where threads from one application need to communicate to multiple threads in another application on the same computer, and the communicating threads can be on different physical CPUs). I assume that it would involve a shared memory...

Write-Only Reference in C++?

Is there a way to code a write-only reference to an object? For example, suppose there was a mutex class: template <class T> class mutex { protected: T _data; public: mutex(); void lock(); //locks the mutex void unlock(); //unlocks the mutex T& data(); //returns a reference to the data, or throws an exception if lock is ...

Multiuser XML document "database" for asp.net app

I was thinking about a way to allow multiple users to get CRUD access to an XML document in an asp.net app. The operations would obviously have to be made under the assumption of a multithreaded environment. For perf reasons, would it make sense to cache the document, and use a mutex on that cached version? When would changes be flush...