mutex

Why is std::mutex taking a long, highly irregular amount of time to be shared?

被刻印的时光 ゝ 提交于 2019-12-30 11:03:50
问题 This code demonstrates that the mutex is being shared between two threads, but one thread has it nearly all of the time. #include <thread> #include <mutex> #include <iostream> #include <unistd.h> int main () { std::mutex m; std::thread t ([&] () { while (true) { { std::lock_guard <std::mutex> thread_lock (m); sleep (1); // or whatever } std::cerr << "#"; std::cerr.flush (); } }); while (true) { std::lock_guard <std::mutex> main_lock (m); std::cerr << "."; std::cerr.flush (); } } Compiled with

spin_lock on non-preemtive linux kernels

孤人 提交于 2019-12-30 10:10:22
问题 I read that on a system with 1 CPU and non preemtive linux kernel (2.6.x) a spin_lock call is equivalent to an empty call, and thus implemented that way. I can't understand that: shouldn't it be equivalent to a sleep on a mutex? Even on non-preemtive kernels interrupt handlers may still be executed for example or I might call a function that would put the original thread to sleep. So it's not true that an empty spin_lock call is "safe" as it would be if it was implemented as a mutex. Is there

named system mutex not recognized

ぐ巨炮叔叔 提交于 2019-12-30 07:08:06
问题 I am trying named system mutex approach to synchronize two processes- a c# windows service a desktop c# app When the mutex is created, process that didn't create the mutex doesn't seem to detect the existing mutex. Below in more detail: Windows service is responsible for creating the mutex(No prefixes-Global/Local etc. Just a normal named system mutex) as follows: Mutex myMutex= null; try { myMutex= Mutex.OpenExisting(myMutexName); } catch (WaitHandleCannotBeOpenedException x) { //doesn't

Is it safe to mix pthread.h and C++11 standard library threading features?

匆匆过客 提交于 2019-12-30 05:36:16
问题 Can I spawn a thread with pthread_create and use std::mutex inside of it safely? I would think that if std::mutex is implemented as a pthread_mutex_t then it would be fine but I don't see this documented anywhere For example: #include <pthread.h> #include <mutex> namespace { std::mutex global_lock; } void* thread_func(void* vp) { // std::mutex used in thread spawned with pthread_create std::lock_guard<std::mutex> guard(global_lock); // critical section return nullptr; } int main() { pthread_t

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

对着背影说爱祢 提交于 2019-12-30 03:20:08
问题 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_with_value(local_x); } } void writer_thread() { while (1) { float local_x = get_new_value_from_somewhere(); // Grab mutex here? x = local_x; // Release mutex? } } My main concern is that a load or

Static pthreads mutex initialization

别等时光非礼了梦想. 提交于 2019-12-30 02:44:33
问题 Using pthreads, how would one, in C, initialize a static array of mutexes? For a single static mutex, it seems I can use PTHREAD_MUTEX_INITIALIZER. But what about an static array of them? As, in for example, #include &ltpthread.h&gt #define NUM_THREADS 5 /*initialize static mutex array*/ static pthread_mutex_t mutexes[NUM_THREADS] = ...? Or must they be allocated dynamically? 回答1: If you have a C99 conforming compiler you can use P99 to do your initialization: static pthread_mutex_t mutexes

Multiple mutex locking strategies and why libraries don't use address comparison

拥有回忆 提交于 2019-12-29 04:17:26
问题 There is a widely known way of locking multiple locks, which relies on choosing fixed linear ordering and aquiring locks according to this ordering. That was proposed, for example, in the answer for "Acquire a lock on two mutexes and avoid deadlock". Especially, the solution based on address comparison seems to be quite elegant and obvious . When I tried to check how it is actually implemented, I've found, to my surprise, that this solution in not widely used. To quote the Kernel Docs -

Multiple mutex locking strategies and why libraries don't use address comparison

£可爱£侵袭症+ 提交于 2019-12-29 04:17:04
问题 There is a widely known way of locking multiple locks, which relies on choosing fixed linear ordering and aquiring locks according to this ordering. That was proposed, for example, in the answer for "Acquire a lock on two mutexes and avoid deadlock". Especially, the solution based on address comparison seems to be quite elegant and obvious . When I tried to check how it is actually implemented, I've found, to my surprise, that this solution in not widely used. To quote the Kernel Docs -

Mutex lock threads

丶灬走出姿态 提交于 2019-12-29 03:21:06
问题 Am new to multi threaded/processs programming. So here's what I need to clarify. Process A code pthread_mutex_lock() pthread_create(fooAPI(sharedResource)) //fooAPI creates another thread with shared resource that shares across processes. pthread_mutex_unlock() With the above pseudo code, is process B able to access sharedResource if mutex is not unlocked? How can I access the sharedResource from process B correctly? Any there any clear visual diagram that explains the relationship between

Is there a `shared_lock_guard` and if not, what would it look like?

故事扮演 提交于 2019-12-29 01:43:11
问题 I wanted to use a std::mutex in my class, and noticed that it isn't copyable. I'm at the bottom level of my library here, so it seems like a terrible idea to have this behaviour. I used std::lock_guard on the std::mutex , but there doesn't seem to be a shared_lock_guard , which would be preferable to provide write-locks-exclusively behaviour. Is this an oversight or trivial to implement myself? 回答1: With C++14 You can use a std::shared_lock and a std::unique_lock to implement read/write