mutex

Mutex for simple data types

痴心易碎 提交于 2019-12-10 14:47:09
问题 I'm pretty new to concurrency, and I'm having trouble deciding on how to use mutexes. At the moment they are sprinkled all over my code where two threads interact. Would this use of mutexes be appropriate? class Foo { public: void SetMember(int n) { AcquireMutex(..); n_ = n; ReleaseMutex(...);} private: Thread() { while(1) { AcquireMutex(..); // Do something with n_ ReleaseMutex(...); } } }; I have quite a few data members that can be read and set from the outside by a different thread, and I

Signalling a condition variable (pthreads)

风流意气都作罢 提交于 2019-12-10 14:21:35
问题 Suppose some condition variable "cond" is associated with a mutex variable "mutex". If a thread is sleeping on cond after calling pthread_cond_wait(&cond,&mutex) , and another thread that has mutex locked is finished, does it matter whether that thread calls pthread_cond_signal(&cond) before or after calling pthread_mutex_unlock(&mutex) ? Does it even need to unlock the mutex at all if it calls pthread_cond_signal(&cond) , since the sleeping thread will acquire the mutex anyway? EDIT:

Boost shared mutex not released after exception thrown

痞子三分冷 提交于 2019-12-10 12:23:47
问题 I came across a strange Boost (v1.38) mutex deadlock in a preexisting .NET (C#, 3.5) application that makes calls to a C++ library. An exception is [properly] thrown at a point after a read lock is obtained and that exception goes unhandled all the way back to the managed .NET code (where it is handled). The next call to the c++ library that attempts to use a setter method hangs on the unique lock aquisition indefinately (presumably the read lock was not released): ntdll.dll

Reader Writer program in C using mutexes and pthreads

自作多情 提交于 2019-12-10 11:56:31
问题 I am stuck on a Reader/Writer problem in C. Can anybody explain me what is happening in the code below. I dont understand how the execution flows after the pthread_create(&tid,NULL,writer,NULL) line. #include<pthread.h> //#include<semaphore.h> #include<stdio.h> #include<stdlib.h> pthread_mutex_t x,wsem; pthread_t tid; int readcount; void intialize() { pthread_mutex_init(&x,NULL); pthread_mutex_init(&wsem,NULL); readcount=0; } void * reader (void * param) { int waittime; waittime = rand() % 5;

C - Guarantee condvars are ready for signalling

天大地大妈咪最大 提交于 2019-12-10 11:45:40
问题 I have a simple application that interfaces with different pieces of hardware. For each piece of hardware, I spawn a pthread_t against a unique monitor function, for a total of 6 threads: 1 manager thread, and 5 worker threads. Each thread has a common initialization routine where it waits for the manager thread to wake it up via: pthread_mutex_lock(&mMutex); pthread_cond_wait(&mMutex, &condVar); pthread_mutex_lock(&mMutex); The main thread then wakes up all of the threads by signalling them

Synchronize threads for pthread_cond_broadcast call

允我心安 提交于 2019-12-10 10:23:06
问题 I have a simple application with a "manager" thread that spawns ten simple "worker" threads. I want all of the "worker" threads to block on the same condition variable (ie: condvar), and I want to manually signal all ten threads to wake up at the same time with a pthread_cond_broadcast call. In the case of my application, it is possible for threads to suffer an error condition and terminate early, so it is possible that not all ten threads make it to the synchronization point. One simple

pthread_mutex_lock __pthread_mutex_lock_full: Assertion failed with robust and 0x4000000

一世执手 提交于 2019-12-10 09:41:48
问题 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::mutex> to lock the shared member data. There is also a BlockingQueue<ConnectionPtr> which contains the input connections. The implementation of the BlockingQueue : template <typename DataType> class BlockingQueue : private boost::noncopyable { public: BlockingQueue() : nblocked(0), stopped(false) { }

Named Lock Collection in C#?

不想你离开。 提交于 2019-12-10 09:35:59
问题 I have multiple threads writing data to a common source, and I would like two threads to block each other if and only if they are touching the same piece of data. It would be nice to have a way to lock specifically on an arbitrary key: string id = GetNextId(); AquireLock(id); try { DoDangerousThing(); } finally { ReleaseLock(id); } If nobody else is trying to lock the same key, I would expect they would be able to run concurrently. I could achieve this with a simple dictionary of mutexes, but

互斥量与信号量

99封情书 提交于 2019-12-10 06:16:33
互斥量(Mutex) 互斥量表现互斥现象的数据结构,也被当作二元信号灯。一个互斥基本上是一个多任务敏感的二元信号,它能用作同步多任务的行为,它常用作保护从中断来的临界段代码并且在共享同步使用的资源。 Mutex 本质上说就是一把锁,提供对资源的独占访问,所以Mutex主要的作用是用于互斥。Mutex对象的值,只有0和1两个值。这两个值也分别代表了 Mutex的两种状态。值为0, 表示锁定状态,当前对象被锁定,用户进程/线程如果试图Lock临界资源,则进入排队等待;值为1,表示空闲状态,当前对象为空闲,用户进程/线程可以 Lock临界资源,之后Mutex值减1变为0。 Mutex可以被抽象为四个操作: - 创建 Create - 加锁 Lock - 解锁 Unlock - 销毁 Destroy Mutex被创建时可以有初始值,表示Mutex被创建后,是锁定状态还是空闲状态。在同一个线程中,为了防止死锁,系统不允许连续两次对Mutex加锁(系统一般会在第二次调用立刻返回)。也就是说,加锁和解锁这两个对应的操作,需要在同一个线程中完成。 不同操作系统中提供的Mutex函数: 动作\系统 Win32 Linyx Solaris 创建 CreateMutex pthread_mutex_init mutex_init 加锁 WaitForSingleObject pthread_mutex

how to set the priority to get the mutex in C/c++

佐手、 提交于 2019-12-10 04:21:07
问题 I have 3 process (equal priority) P1 P2 P3(timer) priority to get the mutex is as follows: P1(1 priority), P2(2 priority), P3(timer)(3 priority) If suppose p3 comes and get the mutex then p2 comes and wait for mutex after that p1 comes and it also wait for mutex if p3 release mutex then p1 should get the mutex not p2 . How to perform this in C or C++. Note : all processes are running inside threads having same priority. OS - windows Xp 回答1: Since the threads have equal priority, which thread