mutex

What is the difference between .Semaphore() and .BoundedSemaphore()?

喜你入骨 提交于 2019-12-21 02:28:30
问题 I know that threading.Lock() is equal to threading.Semaphore(1) . Is also threading.Lock() equal to threading.BoundedSemaphore(1) ? And newly I met threading.BoundedSemaphore() , what is the difference between these? such as the following code snippet (to apply limitation on threads): import threading sem = threading.Semaphore(5) sem = threading.BoundedSemaphore(5) 回答1: A Semaphore can be released more times than it's acquired, and that will raise its counter above the starting value. A

Mutex access and system call

…衆ロ難τιáo~ 提交于 2019-12-20 20:19:09
问题 I know that in Linux mutexes are implemented as futexes down below and futex uses compare-and-swap mechanism. And usually for acquiring locks, a user-space thread does not need to make a system call as the lock is resolved in user-space. Now my question is what happens when there is high contention and many threads are trying to lock a mutex at the same time. Does a system call occurs then for the kernel to decide which thread to grant the mutex? Especially when thread priorities are

What is the consensus number for semaphores?

一笑奈何 提交于 2019-12-20 09:57:34
问题 (I think that) the consensus number for a mutex is 2. What is the consensus number for semaphores (like in pthread_sem_*)? What is the consensus number for condition variables (like in pthread_cond_*)? 回答1: The consensus number for a mutex would be 1. It's trivially clear that a mutex will be wait-free for a single thread. From its definition, it's also clear that a mutex is no longer wait-free for two threads. The consensus number therefore is >=1 and <2, so it must be 1. Likewise, other

What is the Mutex and semaphore In c#? where we need to implement? [closed]

拟墨画扇 提交于 2019-12-20 09:53:38
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . What is the Mutex and semaphore in C#? Where we need to implement? How can we work with them in multithreading? 回答1: You should start at MSDN. System.Threading.Mutex: A synchronization primitive that can also be used for interprocess synchronization. System.Threading.Semaphore:

What is Test-and-Set used for?

。_饼干妹妹 提交于 2019-12-20 09:53:07
问题 After reading the Test-and-Set Wikipedia entry, I am still left with the question "What would a Test-and-Set be used for?" I realize that you can use it to implement Mutex (as described in wikipedia), but what other uses does it have? 回答1: You use it any time you want to write data to memory after doing some work and make sure another thread hasn't overwritten the destination since you started. A lot of lock/mutex-free algorithms take this form. 回答2: A good example is "increment." Say two

What is Test-and-Set used for?

耗尽温柔 提交于 2019-12-20 09:51:17
问题 After reading the Test-and-Set Wikipedia entry, I am still left with the question "What would a Test-and-Set be used for?" I realize that you can use it to implement Mutex (as described in wikipedia), but what other uses does it have? 回答1: You use it any time you want to write data to memory after doing some work and make sure another thread hasn't overwritten the destination since you started. A lot of lock/mutex-free algorithms take this form. 回答2: A good example is "increment." Say two

How to use mutex

≯℡__Kan透↙ 提交于 2019-12-20 07:51:15
问题 Where should i put the lock and unlock mutex in order for the threads to print alternatively? Thanks:D Implement a program that creates two threads. The threads will print their ID (pthread_self) 10 times and then stop. Insure that the printed IDs alternate always (ie A, B, A, B, ...) #include <stdio.h> #include <pthread.h> #define N 2 pthread_mutex_t mtx; void* func (void* arg) { int i=0; int f=1; for(i=0; i<10; i++) { printf("%d%s%d\n",f ,": ", (int)pthread_self()); f++; } return NULL; }

Properly deleting a singleton

只愿长相守 提交于 2019-12-20 06:32:36
问题 I have the following code: MyClass.h: static MyMutex instanceMutex; static MyClass* getInstance(); static void deleteInstance(); MyClass.c: MyMutex MyClass::instanceMutex; MyClass* MyClass::getInstance() { if (theInstance == 0) { instanceMutex.acquire(); if (theInstance == 0) { theInstance = new MyClass(); } instanceMutex.release(); } return theInstance; } void MyClass::deleteInstance() { if (theInstance != 0) { instanceMutex.acquire(); if (theInstance != 0) { theInstance->finalize(); delete

How to provide a sequence of interleaving threads to show that a code breaks and doesn't provide perfect synchronization?

送分小仙女□ 提交于 2019-12-20 06:17:40
问题 I know what the following code does and I know why it is a broken code for synchronization as it has only one conditional variable while we need two but I don't know how to provide a sequence of interleaving threads for showing it doesn't work. Can you show why this code doesn't work with an example? 1 cond_t cond = PTHREAD_COND_INITIALIZER; 2 mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;; 3 4 void *producer(void *arg) { 5 int i; 6 for (i = 0; i < loops; i++) { 7 Pthread_mutex_lock(&mutex); 8

Is there a way to ensure atomicity while having a multithreaded program with signal handlers?

拥有回忆 提交于 2019-12-20 04:29:04
问题 If I have a program like this (in pseudocode): mutex_lock; func() { lock(mutex_lock); // Some code (long enough to make a // race condition if no proper synchronisation // is available). We also going to call a signal, // say, SIGINT, through (ctrl-c), while we are in // the range of locking and unlocking the lock. unlock(mutex_lock); } sig_handler_func(sig) { // Say, we are handling SIGINT (ctrl-c) signal // And we need to call func from here too. if (sig == SIGINT) { func(); } } main() { //