Questions tagged [mutex]

A mutex ("mutual exclusion") is a mechanism to ensure integrity when the same data or resource is accessed (in particular, changed) concurrently from several threads.

mutex
Filter by
Sorted by
Tagged with
3 votes
2 answers
213 views

How can any thread usually release the non-recursive mutex no matter which thread originally took the mutex?

https://stackoverflow.com/a/189778/462608 In the case of non-recursive mutexes, there is no sense of ownership and any thread can usually release the mutex no matter which thread originally took ...
Aquarius_Girl's user avatar
3 votes
1 answer
4k views

Boost, pointer to a mutex, will that work? boost::mutex and std::vector, noncopyable issue

The following code will give me an error, since boost::mutex is noncopyable, while xyz.push_back() is a copy constructor. class XYZ { public: double x; boost::mutex x_mutex; } ...
2607's user avatar
  • 4,105
3 votes
3 answers
3k views

multi-threaded consumer,producer C code, not performing?

I'm trying to do a simple multi-threaded consumer/producer, where multiple reader and writer thread, read from a file to the buffer and then from buffer back into a file. It should be thread safe. ...
user1127217's user avatar
3 votes
2 answers
4k views

How can I protect a vector with a Mutex?

I am working on designing a C++ server that accepts multiple different interacting clients, and I use vectors to keep track of all of them individually. However, I realized that, because of so many ...
Sefu's user avatar
  • 2,444
3 votes
2 answers
604 views

How can I find out which thread has the lock on a Mutex?

I use the .Net Mutex class to lock parts of my app across threads. I'm having some deadlock issues, and it would be very helpful if I could find out the name of the thread that currently has the lock. ...
Mike Pateras's user avatar
  • 14.9k
3 votes
3 answers
7k views

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 /...
user avatar
3 votes
3 answers
2k views

PHP session handling when the same client requests the same script multiple times at once

So here's my test setup: session_start(); if(!isset($_SESSION['bahhhh'])) $_SESSION['bahhhh'] = 0; $_SESSION['bahhhh']++; sleep(5); die('a'.$_SESSION['bahhhh']); What I expect to happen is that ...
VexedPanda's user avatar
3 votes
4 answers
4k views

QMutex stuck in locked state

I have a function which is part of a class and in this function there is a mutex.lock at the beginning of the function and a mutex.unlock just before its return. Now I have encountered a situation ...
yan bellavance's user avatar
3 votes
2 answers
4k views

Condition variables in c++, how do I use them properly?

I am trying to implement a worker object (that is a thread that waits for tasks and terminates only when destroyed), but I am having trouble using the std::condition_variable: #include <thread> #...
ugo_capeto's user avatar
3 votes
6 answers
820 views

Altering and reading elements of a unordered_map from different threads without lock

I have an unordered_map in code, which has a fixed number of elements initialized at the startup of my program std::unorderd_map<int, bool> flags; //Initialize all needed values in the map // ...
whitetiger's user avatar
3 votes
1 answer
172 views

Is there a way to reuse std::atomic on a given int32_t?

I have an int32_t with an initial value 42. Now I want it to be atomically accessed by multiple threads. #include <atomic> using namespace std; int32_t* pn{}; int main() { pn = ...
xmllmx's user avatar
  • 40.9k
3 votes
1 answer
181 views

If a thread locks a mutex and doesn't unlock it, shouldn't be the rest of threads blocked?

The output of the code below is 4000; why it's 4000 if the thread which have the mutex doesn't release it. I though it will be a deadlock, in the main i wait for all the functions to finish. int M = ...
user avatar
3 votes
1 answer
239 views

c# - Waiting for 1 of 2 threads to be finished

I have a place in my code, that I need to wait for a either finger to be identified on a sensor, or the user pressed a key to abort this action and return to the main menu. I tried using something ...
TheSiIence's user avatar
3 votes
3 answers
2k views

Mutex not unblocking when unlocked on separate thread C++

I am trying to understand why the behavior of my mutex is not as I would expect. I was debugging another problem, and decided to make a very simple executable to directly test the mutex behavior. ...
user2445507's user avatar
3 votes
1 answer
2k views

Why do spin locks make no sense on a single CPU?

While reading Operating System Three Easy Pieces I found the following sentence: "To work correctly on a single processor, it requires a preemptive scheduler (i.e., one that will interrupt a thread ...
George Li's user avatar
3 votes
3 answers
6k views

QT signals and slots direct connection behaviour in application with a single thread

I am having difficulty grasping what happens when for eg. two different signals are connected to two different slots and when one slot is not done, the other slots' signal is emitted (for both slots ...
etugcey's user avatar
  • 109
3 votes
2 answers
297 views

Rails 4 Concurrency Issue

I am having an hard time figuring out the reason why my model is behaving like this. It has 2 actions, cancel and feature, when attribute was already canceled it shouldn't be available to be ...
ace's user avatar
  • 305
3 votes
1 answer
2k views

C++ Locking stream operators with mutex

I need to lock stdout in my logging application to prevent string interleaving in multi-thread applications logging to stdout. Can't figure out how to use move constructor or std::move or sth else to ...
MarcusSK's user avatar
3 votes
3 answers
914 views

Why the mutex code stops another whole go-routine?

var m *sync.RWMutex func main() { m = new(sync.RWMutex) n := 100 go func() { for i := 0; i < n; i++ { write("WA", i) } }() go func() { for i ...
hardPass's user avatar
  • 19.9k
3 votes
1 answer
12k views

no reference to pthread_mutex_lock with -lpthread compiled

I am compiling a program that contains mutex semaphores from the pthread library but when i compile using the -lpthread flag I am getting an undefined reference error. gcc -lpthread prodcon.c /tmp/...
aastorms's user avatar
3 votes
2 answers
575 views

Is a Try-Finally-Release redundant while using a Mutex 'using'?

I am currently using a mutex like so (which in turn is a fork of this SO answer): bool onlyInstance; using (Mutex mutex = new Mutex(true, "Global\\mutexname", out onlyInstance)) { try { ...
reggaemahn's user avatar
  • 6,440
3 votes
1 answer
625 views

Is std::mutex sufficient for data synchronization between threads

If I have a global array that multiple threads are writing to and reading from, and I want to ensure that this array remains synchronized between threads, is using std::mutex enough for this purpose, ...
Deep Learner's user avatar
3 votes
1 answer
1k views

Mutex pattern where multiple threads can have read-only access

I have a list of Dinosaur objects, that can be added to, removed from, and the dinosaurs themselves need to be fed. This all happens in a highly multi-threaded environment, so the list is mutex ...
Yellow's user avatar
  • 3,925
3 votes
1 answer
406 views

@synchronized - skip instead of wait

I've got an Objective-c app with several blocks of code where I only want one thread to be able to access it at a time. Using a @synchronized(self) block works fine for that. However, I've got one ...
Troy's user avatar
  • 21.5k
3 votes
1 answer
3k views

C++: std::async and std::mutex leads to deadlock on Linux but runs on Windows?

I just compiled a project I've been working on under Windows for Linux and found that it hangs at a certain point. Since I am using std::async and std::mutex my first assumption was, that it could be ...
bweber's user avatar
  • 3,952
3 votes
1 answer
2k views

What's the difference between pthread_mutex_lock and kernel mutex_lock in linux?

Is there any relation between them? What's the difference on the implementation and the performance?
bydsky's user avatar
  • 1,734
3 votes
3 answers
4k views

what happens if Interrupts occur after mutex lock has been acquired

I have a multi-threaded C/C++ program that services read and write requests very frequently. For synchronization purposes, I have used a mutex for locks and unlocks. All read and write threads acquire ...
user3296247's user avatar
3 votes
1 answer
11k views

InterProcess communication -- Locking Mutex in shared memory

I have 2 processes that will execute the same code: #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <semaphore.h> #include <...
Kam's user avatar
  • 5,908
3 votes
1 answer
3k views

Golang: Best way to read from a hashmap w/ mutex

This is a continuation from here: Golang: Shared communication in async http server Assuming I have a hashmap w/ locking: //create async hashmap for inter request communication type state struct { ...
kwolfe's user avatar
  • 1,663
3 votes
3 answers
3k views

Is there any way to lock a mutex and have it automatically unlock after a given time?

This is on Linux but ideally I'd like a POSIX compliant solution. I have two threads each of which is able to write to a hardware bus via an API function. Only one thread is allowed to write to the ...
Simon Elliott's user avatar
3 votes
1 answer
2k views

Null reference exception in exception logging masking true error

We've got a long-running process that is initiated by a web request. In order to give the process time to complete, we spin it off on a new thread and use a Mutex to ensure only one instance of the ...
The Rover's user avatar
3 votes
1 answer
3k views

Pthread_mutex_lock returning access error

I'm writing a program that reads and processes large amounts of data. To speed up the process I've implemented multithreaded reading/processing using the Pthreads library for C++. When I call ...
LiamK's user avatar
  • 805
3 votes
3 answers
493 views

C++ partial mutex/critical section lock

I am working in C++ in VS2010, and I have a container class with a bunch properties (getters and setters) (actually a bunch of objects with properties – but lets simplify it and assume that it is just ...
Martin Vang's user avatar
3 votes
1 answer
2k views

Boost::mutex performance vs pthread_mutex_t

I was using pthread_mutex_ts beforehand. The code sometimes got stuck. I had a couple of lines of code scattered across functions that I wrapped... pthread_mutex_lock(&map_mutex);// Line 1 //...
Joshua's user avatar
  • 1,576
3 votes
2 answers
17k views

pthread_mutex_lock __pthread_mutex_lock_full: Assertion failed with robust and 0x4000000

I'm working on a server-side project, which is supposed to accept more than 100 client connections. It's multithreaded program using boost::thread. Some places I'm using boost::lock_guard<boost::...
Peter Lee's user avatar
  • 13.4k
3 votes
1 answer
2k views

mutex with PHP 5.3.8

I am using PHP 5.3.8 on a web server running Windows XP SP3 and Apache 2.2.21 where I need to create a mutex. After some research, I've come across the flock command and implemented it like this: ...
PaulH's user avatar
  • 7,819
3 votes
3 answers
2k views

Should I unlock if I call another function inside of a function?

Let's say I have a simple code like this functionA(){ lock(lockA); //doing something with a shared data; functionB(); unLock(lockA); } functionB(){ lock(lockB); //...
codereviewanskquestions's user avatar
3 votes
2 answers
4k views

sharing mutexes between modules

How does one use mutexes between modules? I have a module that creates threads that run functions from another module. I need to read a variable in the thread creating module and the threads that ...
some_id's user avatar
  • 29.6k
3 votes
2 answers
2k views

Distributed critical section in web-farm

I have about 50 web-sites, load-balanced across 5 web-servers. They all use Enterprise Library Caching, and access the same Caching database. The items in the Caching database are refreshed every few ...
RB.'s user avatar
  • 36.7k
3 votes
3 answers
6k views

Usage of Mutex across processes

OS: Windows Language: C/C++ The design demands to use a mutex variable across process and its sub processes. If I create mutex in one process, I have to open the mutex in another processs to check ...
Muthukumar Palaniappan's user avatar
3 votes
2 answers
9k views

How does this implementation of semaphore work?

Merry Xmas! I'm reading the The Little Book of Semaphores. There is an implementation of semaphores in C in the book that I don't completely understand. See below for code. There is this wakeups ...
Jan Deinhard's user avatar
  • 19.9k
3 votes
4 answers
2k views

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 ...
Alex Torres's user avatar
3 votes
4 answers
1k views

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. ...
user avatar
3 votes
3 answers
14k views

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 ...
Khurram Aziz's user avatar
  • 1,480
3 votes
2 answers
91 views

How would you implement a thread-safe function which reads from a shared hashtable using a key and updates the value in a multi-threaded environment?

Suppose we have some function which takes in a key, retreives its value from a shared hashtable, and perform some operations on it to obtain a new value , and then updates the hashtable with this new ...
Lew Wei Hao's user avatar
3 votes
2 answers
4k views

Using a mutex within a struct in Go

I see in Essential Go that using a mutex within a struct is not too straight-forward. To quote from the Mutex Gotchas page: Don’t copy mutexes A copy of sync.Mutex variable starts with the same state ...
user avatar
3 votes
1 answer
280 views

Why can't I put a borrowed value behind a Mutex and pass it to another thread?

I'm trying to put a borrowed value behind a Mutex but I'm having trouble with the borrow checker. Here's a simplified code that demonstrate the problem I encounter: use std::{ marker::PhantomData, ...
hadroncfy's user avatar
3 votes
1 answer
914 views

Does a call to unlock a mutex necessarily switch to another thread that was blocked on the lock?

Let's say we have two threads: th1 and th2. Let's imagine this line of events: Th1 locks the mutex and does some work in it's critical region. Th2 calls lock on the mutex but is blocked. Th1 ...
Stefan Dimeski's user avatar
3 votes
4 answers
2k views

Two Task Synchronisation With Mutex In FreeRTOS

I am trying to turn on and off a led using FreeRTOS on STM32 F401RE MCU in IAR Workbench IDE. The led belongs the STM32 nucleo board. There are two task one turn on the Led, the other task turn off ...
Nazim's user avatar
  • 406
3 votes
2 answers
772 views

How do I force threads to take turn concatenating to a string?

Basically I have 2 text files with each file having a bunch of lines all 1-character long. Each character in one file is a letter or zero, if the character is zero, I need to look at the other file to ...
Drew Pesall's user avatar

1
18 19
20
21 22
95