mutex

Not locking mutex for pthread_cond_timedwait and pthread_cond_signal ( on Linux )

北城以北 提交于 2019-12-28 16:06:13
问题 Is there any downside to calling pthread_cond_timedwait without taking a lock on the associated mutex first, and also not taking a mutex lock when calling pthread_cond_signal ? In my case there is really no condition to check, I want a behavior very similar to Java wait(long) and notify(). According to the documentation, there can be "unpredictable scheduling behavior". I am not sure what that means. An example program seems to work fine without locking the mutexes first. 回答1: The first is

Not locking mutex for pthread_cond_timedwait and pthread_cond_signal ( on Linux )

戏子无情 提交于 2019-12-28 16:06:06
问题 Is there any downside to calling pthread_cond_timedwait without taking a lock on the associated mutex first, and also not taking a mutex lock when calling pthread_cond_signal ? In my case there is really no condition to check, I want a behavior very similar to Java wait(long) and notify(). According to the documentation, there can be "unpredictable scheduling behavior". I am not sure what that means. An example program seems to work fine without locking the mutexes first. 回答1: The first is

pthread mutex not working correctly

℡╲_俬逩灬. 提交于 2019-12-28 06:51:25
问题 I am currently learning C from MIT's Open Courseware course called Practical Programming in C. In discussing race conditions in multithreading, the lecture notes contained an example of a program with a race condition and how it could be resolved using a mutex. The code works as expected on linux systems, but not on OS X. #include <stdio.h> #include <stdlib.h> #include <pthread.h> pthread_mutex_t mutex; // Added to fix race condition unsigned int cnt = 0; void *count(void *arg) { int i; for

Getting exclusive system-wide lock in Java

喜夏-厌秋 提交于 2019-12-28 05:58:48
问题 I'm re-working a Java executable that may be started multiple times, and I want the process to proceed one at a time. In C# I would do this with a named/system Mutex, but this doesn't seem to be possible in Java. How can I achieve this functionality? 回答1: You can use exclusive access to a File on the File System to achieve similar behavior. I don't think there is something similar to what you've mentioned. Examples Java Programming [Archive] - open File in exclusive lock java.nio.channels

How are mutexes implemented?

倖福魔咒の 提交于 2019-12-28 02:26:10
问题 Are some implementations better than others for specific applications? Is there anything to earn by rolling out your own? 回答1: Check out the description of the Test-and-set machine instruction on Wikipedia, which alludes to how atomic operations are achieved at the machine level. I can imagine most language-level mutex implementations rely on machine-level support such as Test-and-set. 回答2: Building on Adamski's test-and-set suggestion, you should also look at the concept of "fast user-space

Working with Slices and Golang sync.Map Structure

孤街浪徒 提交于 2019-12-25 19:37:49
问题 In order to debug some concurrency issues, I am in the process of switching part of my code from working on a regular Golang map to working on a sync.Map. However, when I try to run my new code, I am encountering two errors that I'm not sure how to debug. The original code block: sync_mutex.Lock() if _, ok := the_map[cur_h]; ok { the_map[cur_h] = append(the_map[cur_h], cur_id) } else { value := []int{cur_id} the_map[cur_h] = value } sync_mutex.Unlock() The new code block: if _, ok := sync_map

Working with Slices and Golang sync.Map Structure

末鹿安然 提交于 2019-12-25 19:37:33
问题 In order to debug some concurrency issues, I am in the process of switching part of my code from working on a regular Golang map to working on a sync.Map. However, when I try to run my new code, I am encountering two errors that I'm not sure how to debug. The original code block: sync_mutex.Lock() if _, ok := the_map[cur_h]; ok { the_map[cur_h] = append(the_map[cur_h], cur_id) } else { value := []int{cur_id} the_map[cur_h] = value } sync_mutex.Unlock() The new code block: if _, ok := sync_map

Working with Slices and Golang sync.Map Structure

☆樱花仙子☆ 提交于 2019-12-25 19:37:17
问题 In order to debug some concurrency issues, I am in the process of switching part of my code from working on a regular Golang map to working on a sync.Map. However, when I try to run my new code, I am encountering two errors that I'm not sure how to debug. The original code block: sync_mutex.Lock() if _, ok := the_map[cur_h]; ok { the_map[cur_h] = append(the_map[cur_h], cur_id) } else { value := []int{cur_id} the_map[cur_h] = value } sync_mutex.Unlock() The new code block: if _, ok := sync_map

What happens to mutex acquirement in wait()/signal() block?

被刻印的时光 ゝ 提交于 2019-12-25 18:42:26
问题 So the description of the exercise: You have this restaurant in which there are N points where you can ask for a portion of fries. Each point has M portions. There is 1 frycheff. When an order-point has 2 portions, it warns the cheff that it needs a refill. The cheff delivers the portions in order of FIFO. We made this pseudo-code: init { Semafoor[] mutex; Condition[] cond_point = new Condition[N]; int[] portions = new int[N]; ArrayList<int> waitline = new ArrayList<int>(); for(int i = 0; i <

Semaphore signalling vs mutex

吃可爱长大的小学妹 提交于 2019-12-25 09:33:13
问题 I was looking over some topics that describes the difference between mutex and binary semaphore. In many topics it is stated that semaphore act as a signalling mechanism i.e if a thread has locked a semaphore then another thread can unlock(release) the semaphore(acting as signal). But in case of mutex only the thread that locks the mutex can unlock it .It can't be unlocked by any other thread and if other thread tries to unlock it this will return error. I have tried writing a code that uses