mutex

Advantages of using condition variables over mutex

拈花ヽ惹草 提交于 2019-12-18 10:00:11
问题 I was wondering what is the performance benefit of using condition variables over mutex locks in pthreads. What I found is : "Without condition variables, the programmer would need to have threads continually polling (possibly in a critical section), to check if the condition is met. This can be very resource consuming since the thread would be continuously busy in this activity. A condition variable is a way to achieve the same goal without polling." (https://computing.llnl.gov/tutorials

implement mutex in node.js

匆匆过客 提交于 2019-12-18 09:21:50
问题 I would like to implement a mutex inside my node.js application, here is the mutex in wiki http://en.wikipedia.org/wiki/Mutual_exclusion. Is there any ready module for this topic? if not, any idea can help me to implement it? 回答1: There are many ways to accomplish this. Two easy ways are via Redis or Zookeeper servers. Node.js has very good modules for both of them. In Redis you can use WATCH + MULTI commands to implement locking. In Zookeeper you can create ephemeral nodes. In both way no

How to use a mutex

試著忘記壹切 提交于 2019-12-18 09:06:37
问题 I have one thread, that is sending data stored in a buffer of type List< string> via tcp. Another thread is writing into the buffer. As I am not very familiar with c# I'd like to know how I should use lock or Mutex correctly. This is the code I'd like to use eventually: while(buffer.isLocked()) { buffer.wait(); } buffer.lockBuffer(); buffer.add(tcpPacket); buffer.unlockBuffer(); buffer.notify(); This is my current code. I hope someone can help me complete it. public class Buffer { private

Is it safe to call pthread_mutex_lock before pthread_mutex_init?

て烟熏妆下的殇ゞ 提交于 2019-12-18 05:53:33
问题 I've never had the chance to play with the pthreads library before, but I am reviewing some code involving pthread mutexes. I checked the documentation for pthread_mutex_lock and pthread_mutex_init , and my understanding from reading the man pages for both these functions is that I must call pthread_mutex_init before I call pthread_mutex_lock . However, I asked a couple colleagues, and they think it is okay to call pthread_mutex_lock before calling pthread_mutex_init . The code I'm reviewing

Is it safe to call pthread_mutex_lock before pthread_mutex_init?

牧云@^-^@ 提交于 2019-12-18 05:53:13
问题 I've never had the chance to play with the pthreads library before, but I am reviewing some code involving pthread mutexes. I checked the documentation for pthread_mutex_lock and pthread_mutex_init , and my understanding from reading the man pages for both these functions is that I must call pthread_mutex_init before I call pthread_mutex_lock . However, I asked a couple colleagues, and they think it is okay to call pthread_mutex_lock before calling pthread_mutex_init . The code I'm reviewing

Why is there no wait function for condition_variable which does not relock the mutex

僤鯓⒐⒋嵵緔 提交于 2019-12-18 04:59:14
问题 Consider the following example. std::mutex mtx; std::condition_variable cv; void f() { { std::unique_lock<std::mutex> lock( mtx ); cv.wait( lock ); // 1 } std::cout << "f()\n"; } void g() { std::this_thread::sleep_for( 1s ); cv.notify_one(); } int main() { std::thread t1{ f }; std::thread t2{ g }; t2.join(); t1.join(); } g() "knows" that f() is waiting in the scenario I would like to discuss. According to cppreference.com there is no need for g() to lock the mutex before calling notify_one .

what is the “attribute” of a pthread mutex?

痴心易碎 提交于 2019-12-17 23:25:41
问题 The function pthread_mutex_init allows you to specify a pointer to an attribute. But I have yet to find a good explanation of what pthread attributes are. I have always just supplied NULL. Is there a use to this argument? The documentation, for those of you who forget it: PTHREAD_MUTEX_INIT(3) BSD Library Functions Manual PTHREAD_MUTEX_INIT(3) NAME pthread_mutex_init -- create a mutex SYNOPSIS #include <pthread.h> int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr

Usage of Mutex in c#

自作多情 提交于 2019-12-17 23:15:24
问题 I am a bit new in threading in c# and on general, in my program I am using mutex to allow only 1 thread getting inside a critical section and for unknown reason with doing some cw prints I can see that more than 1 thread is getting inside my critical section and this is my code : Mutex m = new Mutex(); m.WaitOne(); <C.S> // critical section here m.ReleaseMutex(); I would very much like to know if I am doing a mistake here thanks in advance for your kind help. EDIT: My code include classes so

How To Use Condition Variable

99封情书 提交于 2019-12-17 22:52:21
问题 The Linux Programming Interface book has a piece of code (producer/consumer) to show how condition variable works: static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; static int avail = 0; while (TRUE) { s = pthread_mutex_lock(&mtx); while (avail == 0) { /* Wait for something to consume */ s = pthread_cond_wait(&cond, &mtx); } while (avail > 0) { /* Consume all available units */ avail--; } s = pthread_mutex_unlock(&mtx); } Why we use

Is std::mutex fair? [duplicate]

邮差的信 提交于 2019-12-17 21:32:00
问题 This question already has an answer here : Do mutex locks happen in the same order they are asked? (1 answer) Closed 6 years ago . As the question states, is std::mutex fair? i.e., if thread A locked the mutex and then B and C call 'lock()' on it in this order, will they obtain the lock on the mutex in this same order or is the order unspecified? The documentation doesn't address this at all. 回答1: The standard (§30.4) does not mention anything about requirements regarding fairness between