mutex

android NDK mutex locking

谁都会走 提交于 2019-12-04 08:24:02
I've been porting a cross platform C++ engine to Android, and noticed that it will inexplicably (and inconsistently) block when calling pthread_mutex_lock . This engine has already been working for many years on several platforms, and the problematic code hasn't changed in years, so I doubt it's a deadlock or otherwise buggy code. It must be my port to Android.. So far there are several places in the code that block on pthread_mutex_lock. It isn't entirely reproducible either. When it hangs, there's no suspicious output in LogCat. I modified the mutex code like this (edited for brevity... real

read/write lock implementation using mutex only?

試著忘記壹切 提交于 2019-12-04 08:22:19
问题 I was trying to implement read/write lock using mutex only (just for learning). Just when i thought i have covered all corner cases (as the program worked with variety of combinations), i have realized, i ignored the fact (as it worked in ubuntu) that, the mutex should be freed by the owner of the thread. Below is my implementation, class rw_lock_t{ int NoOfReaders; int NoOfWriters, NoOfWritersWaiting; pthread_mutex_t class_mutex; pthread_cond_t class_cond; pthread_mutex_t data_mutex; public:

How to profile pthread mutex in linux?

夙愿已清 提交于 2019-12-04 08:19:21
问题 I would like to know how to profile a pthread mutex to see if there are any locking contention points in my code. (who likes contentious code, right? :) I know how to do a more general profiling of the code, as I mention here. But I would like to know if there are any tools or options available to be able to profile mutex locking that would provide metrics/stats about mutex locking contentions to see if I have any problem areas. Here's some background and context: Recently I worked on an

When are lock free data structures less performant than mutual exclusion (mutexes)?

大兔子大兔子 提交于 2019-12-04 07:44:22
问题 I read somewhere (can't find the page anymore) that lock free data structures are more efficient "for certain workloads" which seems to imply that sometimes they're actually slower or the gain from them can be zero in some situations. Taking the ~100 cycle hit of a lock instruction to do an atomic op sounds plenty faster to me than going to sleep and waiting for the scheduler to wake the process back up, so it's not obvious to me under what circumstances a lock free data structure would be

Java: What, if anything, is locked by synchronized methods apart from the object they belong to?

[亡魂溺海] 提交于 2019-12-04 07:38:18
Now, I'm not sure whether this is a stupid question, please bear with me if it is. Is the lock on an object "recursive", i. e. if two objects have references to a third object in their fields and a thread is running a synchronized method on one of the two, can any other thread access the third object? // a and b are some objects that implement Runnable // they both reference the same third object a.ref = c; b.ref = c; // a is run in a thread and processes some data in a loop for a long time // the method the loop belongs to is declared synchronized threadA = new Thread(a); threadA.start(); a

ARM cortex: mutex using bit banding

爷,独闯天下 提交于 2019-12-04 07:35:22
Given that, on the ARM Cortex M3, I can: atomically read a single bit atomically set a single bit atomically clear a single bit How can I combine these for a mutex style set of operations: try lock take lock release lock It seems that try_lock or take_lock would require two operations that would not be atomic. Do I need more control to accomplish this? Disable global interrupts would do it but it seems there should be a more surgical approach. Your rwl_TryLock() doesn't necessarily return a failure if the lock is already held when it's called (your compiler should be giving at least a warning

Using mutexes/semaphores with processes

落爺英雄遲暮 提交于 2019-12-04 07:18:45
Almost all the code and tutorials that I have read online so far involve using mutexes and semaphores for synchronisation amongst threads. Can they be used to synchronise amongst processes? I'd like to write code that looks like this: void compute_and_print() { // acquire mutex // critical section // release mutex } void main() { int pid = fork(); if ( pid == 0 ) { // do something compute_and_print(); } else { // do something compute_and_print(); } } Could someone point me towards similar code that does this? I understand that different processes have different address spaces, but I wonder if

What does 'Mutex lock' exactly do?

我们两清 提交于 2019-12-04 06:21:06
You can see an interesting table at this link. http://norvig.com/21-days.html#answers The table described, Mutex lock/unlock 25 nanosec fetch from main memory 100 nanosec Nanosec? I surprised because mutex lock is faster than fetch data from memory . If so, what mutex lock exactly do? And what does Mutex lock mean at the table? Andrew Savinykh The article you linked does not mentioned the architecture, but judging by mentions of L1 and L2 cache it's Intel. If this is so, then I think that by mutex they meant LOCK instruction. In this respect this post seems relevant: Intel 64 and IA-32 |

C++ atomic with non-trivial type?

非 Y 不嫁゛ 提交于 2019-12-04 05:19:29
Reading the docs on boost::atomic and on std::atomic leaves me confused as to whether the atomic interface is supposed to support non-trivial types? That is, given a (value-)type that can only be written/read by enclosing the read/write in a full mutex, because it has a non-trivial copy-ctor/assignment operator, is this supposed to be supported by std::atomic (as boost clearly states that it is UB). Am I supposed to provide the specialization the docs talk about myself for non-trivial types? Note: I was hitting on this because I have a cross-thread callback object boost::function<bool (void)>

Java inter-process mutex

£可爱£侵袭症+ 提交于 2019-12-04 04:21:27
问题 I need to implement some kind of inter-process mutex in Java. I'm considering using the FileLock API as recommended in this thread. I'll basically be using a dummy file and locking it in each process. Is this the best approach? Or is something like this built in the standard API (I can't find it). For more details see below: I have written an application which reads some input files and updates some database tables according to what it finds in them (it's more complex, but business logic is