mutex

Will a thread waiting on a mutex get the ownership, immediately after mutex_unlock() by other thread?

断了今生、忘了曾经 提交于 2019-12-11 06:08:15
问题 I have two threads - threadA & threadB. If B is waiting for mutex, which is owned by A, Will it get the ownership immediately after A unlocks it, assuming it has higher priority than A ? This not a question on who gets the lock when multiple threads are waiting, but if a single waiting thread becomes runnable & gets the processor or not. From the test example below it doesn't seem to happen always. Can someone please clarify ? #include <stdio.h> #include <stdlib.h> #include <unistd.h>

Implementing producers/consumers using mutex

旧巷老猫 提交于 2019-12-11 06:04:48
问题 #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <semaphore.h> #define WORK_SIZE 1024 pthread_mutex_t work_mutex; char work_area[WORK_SIZE]; void *thread_start(void *); int main() { pthread_t a_thread; pthread_mutex_init(&work_mutex,NULL); pthread_create(&a_thread,NULL,thread_start,NULL); while(1) { pthread_mutex_lock(&work_mutex); printf("Enter some text\n"); fgets(work_area, WORK_SIZE, stdin); pthread_mutex_unlock(&work_mutex); }

Using volatile variables and semaphores - Java

。_饼干妹妹 提交于 2019-12-11 05:17:16
问题 I'm starting with Threads, Semaphores, volatile variables, etc. I wonder if when I'm using Semaphores it is necessary to define the variable as volatile, I mean: Having 2 Threads, one increases and the other decreases the variable for example and obviously, before each access I have a mutex that controls at any time only one thread is "playing" with the variable. It would be necessary to define as volatile? 回答1: From API doc of Semaphore: Memory consistency effects: Actions in a thread prior

std::mutex in shared memory not working

随声附和 提交于 2019-12-11 04:45:08
问题 I have a scenario where the shared memory area is exclusively accessed by two different processes. When I launch the processes, the first process successfully locks the mutex, updates the memory and unlock the mutex. But I observe that when the second process try to lock it, it is still in deadlock state, waiting for mutex to unlock. Time difference between the mutex lock is 10s for first and second process. I am using the std::mutex. Please tell me what I am missing. 回答1: An std::mutex

Slightly complicated thread synchronization

空扰寡人 提交于 2019-12-11 04:15:22
问题 Background info: I am Trying to create a C program that allows me to search through several different files (2 in the source) for the largest prime number. The program is multi-threaded to speed up the process. In this program I am preferring computational time rather that wasted time by having a the threads wait until all of the threads have assigned globalLargestPrime. The problem: I believe that in my program somewhere either the id is not being passed as a parameter properly, or that

How to prevent multiple users from adding an item to a Sharepoint list simultaneously

六月ゝ 毕业季﹏ 提交于 2019-12-11 04:03:06
问题 I am using a simple form to allow people to sign up for an event. Their details are saved to a Sharepoint list. I have a quota of people who can sign up for an event (say 100 people). How can I prevent the 100th and the 101st person from signing up concurrently, causing the quota check to allow the 101st person to sign up (because the 100th person isn't in the list yet)? 回答1: Place the ItemAdding code inside a lock statement to make sure that only one thread at a time can enter the critical

Necessity of pthread mutex

折月煮酒 提交于 2019-12-11 03:57:57
问题 I have an int array[100] and I want 5 threads to calculate the sum of all array elements. Each thread iterates through 20 elements within its dedicated range and writes the sum into a global sum variable. Is a mutex necessary here? There is no synchronization needed since all threads are reading from independent sources. for(i=offset; i<offset+range; i++){ // not used pthread_mutex_lock(&mutex); sum += array[i]; // not used pthread_mutex_unlock(&mutex); } Can this lead to unpredictable

Cannot lock a c++ 11 std::mutex on a DLL [duplicate]

限于喜欢 提交于 2019-12-11 02:48:09
问题 This question already has answers here : C++11 std::mutex in Visual Studio 2012 deadlock when locked from DllMain() (2 answers) Closed 5 years ago . I'm trying to prevent multiple calls to a DLL initialization function by using a std::lock object. While using a program like this on a stand alone program works: #include <mutex> std::mutex mtx; void main(){ mtx.lock(); (...) mtx.unlock() } This exact same code cannot get past the mtx.lock() when called on a DLL. BOOL APIENTRY DllMain(HANDLE

AbandonedMutexException for system mutex not caught on 2nd run when 1st run killed

好久不见. 提交于 2019-12-11 02:44:17
问题 I have created the following test program: static void Main(string[] args) { using (var mutex = new Mutex(false, "foobar")) { Console.WriteLine("Created mutex"); try { try { if (!mutex.WaitOne(TimeSpan.FromSeconds(5), false)) { Console.WriteLine("Unable to acquire mutex"); Environment.Exit(0); } } catch (AbandonedMutexException) { Console.WriteLine("Mutex was abandoned"); } Console.WriteLine("Acquired mutex - sleeping 10 seconds"); Thread.Sleep(10000); } finally { mutex.ReleaseMutex();

Modelling boost::Lockable with semaphore rather than mutex (previously titled: Unlocking a mutex from a different thread)

若如初见. 提交于 2019-12-11 01:48:34
问题 I'm using the C++ boost::thread library, which in my case means I'm using pthreads. Officially, a mutex must be unlocked from the same thread which locks it, and I want the effect of being able to lock in one thread and then unlock in another. There are many ways to accomplish this. One possibility would be to write a new mutex class which allows this behavior. For example: class inter_thread_mutex{ bool locked; boost::mutex mx; boost::condition_variable cv; public: void lock(){ boost::unique