mutex

Is mutex needed for different offsets into allocated heap memory

喜欢而已 提交于 2019-12-23 08:06:34
问题 I am laying in the framework for a tool that will generate a binary data table. I a plan on making this multithreaded to take full advantage of the 24 cores at my disposal. (I am estimating that the wall time for generation of the data will be about 50 days–in a single thread.). I have done this in the past using server/client design with socket communication as I needed to distributed this across multiple machines. This time around, I am looking at a single machine/multi-threaded approach

Locking mutex in one thread and unlocking it in the other

拟墨画扇 提交于 2019-12-23 07:28:39
问题 Will this code be correct and portable? void* aThread(void*) { while(conditionA) { pthread_mutex_lock(mutex1); //do something pthread_mutex_unlock(mutex2); } } void* bThread(void*) { while(conditionB) { pthread_mutex_lock(mutex2); //do something pthread_mutex_unlock(mutex1); } } In the actual application I have three threads - two for adding values to an array and one for reading them. And I need the third thread to display the contents of the array right after one of the other threads adds a

spinlock initialization function

Deadly 提交于 2019-12-23 04:00:47
问题 To initialize a spinlock in kernel v4.19-rc5 one must use the spin_lock_init macro defined as follows: #define spin_lock_init(_lock) \ do { \ spinlock_check(_lock); \ raw_spin_lock_init(&(_lock)->rlock); \ } while (0) The function spinlock_check(_lock) just return &lock->rlock . This article explains that: The implementation of the spinlock_check is pretty easy, this function just returns the raw_spinlock_t of the given spinlock to be sure that we got exactly normal raw spinlock I dont't

Why ApplicationException is thrown?

允我心安 提交于 2019-12-23 03:06:58
问题 I am just experimenting on Mutex and wrote the following code. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace Mutex_WaitOnewithTimeouts { class Program { private static Mutex mut = new Mutex(); private static int numOfThreads = 5; private static int numOfIterations = 3; private static Random rand = new Random(); static void Main(string[] args) { Thread[] threads = new Thread[5]; for (int num = 0; num < numOfThreads; num+

Why is performance of pthread_mutex so bad on Mac OS X compared to Linux?

家住魔仙堡 提交于 2019-12-23 02:31:49
问题 I am learning about multi-thread programming right now, and I noticed that programs with synchronization implemented with mutex is extremely slow on Mac OS X, to the extent it is usually better to use single thread instead. I understand that there are much faster ways of synchronizing, but I still wonder why it is like this. For a simple time measurement, I wrote this program. #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <sys/time.h> pthread_mutex_t lock; long s;

Mutexes vs Monitors - A Comparison

╄→гoц情女王★ 提交于 2019-12-22 15:04:33
问题 From what I have learned about Mutexes - they generally provide a locking capability on a shared resources. So if a new thread wants to access this locked shared resource - it either quits or has to continually poll the lock (and wastes processor cycles in waiting for the lock). However, a monitor has condition variables which provides a more asynchronous way for waiting threads - by putting them on wait queue and thereby not making them consume processor cycles. Would this be the only

How can one implement a thread-safe wrapper to maps in Go by locking?

a 夏天 提交于 2019-12-22 11:27:38
问题 I'm trying to wrap a general map (with interface{} as both key and value) as in-memory key-value store that I named MemStore . But it is not thread-safe, despite my use of a sync.RWMutex to lock access to the underlying map. I did verify that it works fine when used from a single goroutine. However, just two concurrent goroutines accessing it results in panic: runtime error: invalid memory address or nil pointer dereference . What is causing this problem, and what is the proper way to achieve

pthread_cleanup_push causes Syntax error

爷,独闯天下 提交于 2019-12-22 10:42:44
问题 I try to add a section to my code which is able to unlock the mutex in a case of cancellation. This may happen and would cause a deadlock. Therefore I tried to add pthread_cleanup_push(cleanup_unlock_mutex, &mutex_ftdi); but this line cause a syntax error from the line where I add it till the end of the code file. If I comment the code line the program will compile without any error. What I'm doing wrong? void cleanup_unlock_mutex(void *p){ pthread_mutex_unlock(p); } ....... }else{ for

Mutex and Windows Phone 8.1

随声附和 提交于 2019-12-22 09:42:08
问题 Here's my problem. Windows Phone 8.1 Visual Studio 2013 Release 4 I've got a main project, and a background project that runs every 30 minutes. I want to pass data between the two. I want to ensure exclusive access to storage in Windows.Storage.ApplicationData.Current.LocalSettings, so I'm using a Mutex. In my main XAML project, I create and use a Mutex named "B+DBgMu" (don't ask). public static Mutex Mu = null; // A Mutex Mu = new Mutex (true, "B+DBgMu"); // Create a Mutex. This is done only

Is it a good idea to use the existence of a named mutex as an indicator?

纵饮孤独 提交于 2019-12-22 08:12:13
问题 I'm using a named mutex to detect other instances of my application and exit accordingly, and found that there are two ways of doing this: Create the mutex; ignore the indication whether it already existed; try to acquire it; use the fact that acquire succeeded/failed. Create the mutex; use the indication whether it already existed. I can't decide whether to acquire the mutex (and release on exit). On the one hand, acquiring+releasing even though it makes no known difference looks like cargo