mutex

C++: Preventing multiple functions from being executed at the same time

雨燕双飞 提交于 2019-12-24 00:58:56
问题 I ask this questions as all the mutex documentations I find deal with a single function and I think my case is very common. My question is whether the following code won't only prevent func1() or func2() from being executed multiple times in parallel, but whether it would also prevent func1() and func2() from being executing at the same time? #include <mutex> std::mutex my_mutex; void func1() { my_mutex.lock(); // do something ... my_mutex.unlock(); } void func2() { my_mutex.lock(); // do

Alternating routines sharing a mutex

爷,独闯天下 提交于 2019-12-24 00:27:18
问题 I have method a that is invoked repeatedly at some random time, which triggers method b , which is completely executed after some random time and is in it own thread. I want to ensure that a subsequent execution of a waits until b is completed, which is triggered by the current execution of a . In other words, a and b are to be executed alternatively. I tried to do this using mutex and condition variable as follows: def a Thread.new do $mutex.synchronize do puts "a" b $cv.wait($mutex) end end

C++ Locking stream operators with mutex

耗尽温柔 提交于 2019-12-24 00:26:00
问题 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 move unique_lock to another object. I created objects for setting configs and encapsulation and figured out how to lock stdout with static std::mutex to lock from these objects (called shards). Something like this works for me: l->log(1, "Test message 1"); While that is fine and could be

One producer, Two consumers and usage of pthread_cond_signal & pthread_mutex_lock

不羁的心 提交于 2019-12-24 00:06:30
问题 I am fairly new to pthread programming and am trying to get my head around cond_signal & mutex_lock . I am writing a sample program which has One producer thread and Two consumer threads. There is a queue between producer and the first consumer and a different queue between producer and the second consumer. My producer is basically a communication interface which reads packets from the network and based on a configured filter delivers the packets to either of the consumers. I am trying to use

Deleting a mutex that is locked

徘徊边缘 提交于 2019-12-23 22:56:42
问题 I have a program with multiple resources that needs to be lock by their own mutex. In this program, it might happen that while mutex for resource A is locked, resource A is deleted in another thread. The following code try to reproduce the logic of what I try to accomplish : #include <thread> #include <mutex> #include <iostream> #include <map> int g_i = 0; struct Resource { std::mutex* m_mutex; }; std::map<unsigned int, Resource> myResources; std::mutex g_i_mutex; // protects g_i void

Is there PTHREAD_MUTEX_ROBUST equivalent in Mac OS X?

混江龙づ霸主 提交于 2019-12-23 19:26:27
问题 I'm using a pthread_mutex_t with PTHREAD_PROCESS_SHARED on a shared memory to do synchronization between different processes. The mutex maybe deadlocked if a process exits but leaves the mutex locked. There is a PTHREAD_MUTEX_ROBUST in POSIX standard. But it seems that Mac OS X does not support the PTHREAD_MUTEX_ROBUST . Is there some kind of mutex on Mac OS X that can be used on a shared memory, and to be used to synchronize cross processes, and to be robust in case of a process die without

Thread mutex behaviour

亡梦爱人 提交于 2019-12-23 19:25:44
问题 I'm learning C. I'm writing an application with multiple threads; I know that when a variable is shared between two or more threads, it is better to lock/unlock using a mutex to avoid deadlock and inconsistency of variables. This is very clear when I want to change or view one variable. int i = 0; /** Global */ static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /** Thread 1. */ pthread_mutex_lock(&mutex); i++; pthread_mutex_unlock(&mutex); /** Thread 2. */ pthread_mutex_lock(&mutex); i

How can I protect a vector with a Mutex?

混江龙づ霸主 提交于 2019-12-23 16:46:13
问题 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 threads running, there's a tiny chance a vector might be read and written to at the same time by two threads. Is there a quick and safe way to add a mutex or something to them so that it will wait until all the reads are done until another function adds to it? Not doing so can mess up the protocol

thrd_busy and mtx_lock()/mtx_timedlock()

不羁的心 提交于 2019-12-23 13:08:59
问题 I have the following questions about C1x mutexes (§7.25.4): In which situations can mtx_lock() return thrd_busy instead of blocking? In which situations can mtx_timedlock() return thrd_busy ? Note that thrd_busy is defined in §7.25.1 ¶5 as being returned " when a resource requested by a test and return function is already in use ". I would expect thrd_busy to be only returned by mtx_trylock() , or at most also by mtx_lock() when invoked with a mtx_try or mtx_try | mtx_recursive mutex, but

Order of execution of waiting threads blocked by mutex

烈酒焚心 提交于 2019-12-23 09:52:25
问题 I have a mutex that controls access to a single object from multiple threads. When a thread has finished the mutex is unlocked to allow order threads to operate on the object. On Windows using the WaitForSingleObject function is there an order that threads are signaled? I want the first thread that attempts to lock the mutex to now be allowed to lock the mutex. This would be a FIFO queue so that signaling to the blocked threads is not random. Would I have to implement my own queuing mechanism