critical-section

Is it valid to nest a critical section?

南楼画角 提交于 2019-11-27 17:28:23
问题 For example, would this be valid? CRITICAL_SECTION cs; ::InitializeCriticalSection( &cs ); ::EnterCriticalSection( &cs ); // First level ::EnterCriticalSection( &cs ); // Second level /* do some stuff */ ::LeaveCriticalSection( &cs ); // Second level ::LeaveCriticalSection( &cs ); // First level ::DeleteCriticalSection( &cs ); Obviously, I would never intentionally do this, but what if this were to come about as a result of function calls such that the "first level" gets called to lock an

Fair critical section (Linux)

 ̄綄美尐妖づ 提交于 2019-11-27 16:56:29
问题 On a multi-threaded Linux application I use a mutex for critical sections. This works very well except for the fairness issue. It can happen that a thread leaving a critical section and re-entering right away does not give any other thread a chance. For example while(true) { critsect.enter(); ... do calculations ... ... maybe call a blocking operation so we sleep ... critsect.leave(); } might very likely stop any other thread to enter the same critical section. Mutexe are not fair. Is there a

Implementing a critical section in CUDA

南楼画角 提交于 2019-11-27 09:15:06
I'm trying to implement a critical section in CUDA using atomic instructions, but I ran into some trouble. I have created the test program to show the problem: #include <cuda_runtime.h> #include <cutil_inline.h> #include <stdio.h> __global__ void k_testLocking(unsigned int* locks, int n) { int id = threadIdx.x % n; while (atomicExch(&(locks[id]), 1u) != 0u) {} //lock //critical section would go here atomicExch(&(locks[id]),0u); //unlock } int main(int argc, char** argv) { //initialize the locks array on the GPU to (0...0) unsigned int* locks; unsigned int zeros[10]; for (int i = 0; i < 10; i++

Is there a difference between Boost's scoped mutex and WinAPi's critical section?

给你一囗甜甜゛ 提交于 2019-11-27 05:47:52
问题 In Windows environment, is Boost's scoped mutex using WinAPI's critical sections, or something else? 回答1: The current version of boost::mutex uses neither a Win32 CRITICAL_SECTION , nor a Win32 Mutex. Instead, it uses atomic operations and a Win32 Event for blocking waits. Older versions (boost 1.34.1 and prior) were a wrapper around CRITICAL_SECTION on Windows. Incidentally, the mutex itself is not scoped. The boost::mutex::scoped_lock type and, in recent versions, boost::lock_guard<boost:

What is the purpose of the “PAUSE” instruction in x86?

瘦欲@ 提交于 2019-11-27 03:16:29
I am trying to create a dumb version of a spin lock. Browsing the web, I came across a assembly instruction called "PAUSE" in x86 which is used to give hint to a processor that a spin-lock is currently running on this CPU. The intel manual and other information available state that The processor uses this hint to avoid the memory order violation in most situations, which greatly improves processor performance. For this reason, it is recommended that a PAUSE instruction be placed in all spin-wait loops. The documentation also mentions that "wait(some delay)" is the pseudo implementation of the

Confusion about the lock statement in C#

笑着哭i 提交于 2019-11-26 20:35:37
问题 This is from MSDN: The lock keyword ensures that one thread does not enter a critical section of code while another thread is in the critical section . Does a critical section have to be same as the critical section ? Or does it mean: The lock keyword ensures that one thread does not enter any critical section guarded by an object of code while another thread is in any critical section guarded by the same object . ? class Program { static void Main(string[] args) {

How to use lock in OpenMP?

╄→гoц情女王★ 提交于 2019-11-26 20:27:00
I have two pieces of C++ code running on 2 different cores. Both of them write to the same file. How to use OpenMP and make sure there is no crash? You want the OMP_SET_LOCK / OMP_UNSET_LOCK functions: https://computing.llnl.gov/tutorials/openMP/#OMP_SET_LOCK . Basically: omp_lock_t writelock; omp_init_lock(&writelock); #pragma omp parallel for for ( i = 0; i < x; i++ ) { // some stuff omp_set_lock(&writelock); // one thread at a time stuff omp_unset_lock(&writelock); // some stuff } omp_destroy_lock(&writelock); Most locking routines such as pthreads semaphores and sysv semaphores work on

Implementing a critical section in CUDA

有些话、适合烂在心里 提交于 2019-11-26 17:49:05
问题 I'm trying to implement a critical section in CUDA using atomic instructions, but I ran into some trouble. I have created the test program to show the problem: #include <cuda_runtime.h> #include <cutil_inline.h> #include <stdio.h> __global__ void k_testLocking(unsigned int* locks, int n) { int id = threadIdx.x % n; while (atomicExch(&(locks[id]), 1u) != 0u) {} //lock //critical section would go here atomicExch(&(locks[id]),0u); //unlock } int main(int argc, char** argv) { //initialize the

What is the purpose of the “PAUSE” instruction in x86?

坚强是说给别人听的谎言 提交于 2019-11-26 08:03:00
问题 I am trying to create a dumb version of a spin lock. Browsing the web, I came across a assembly instruction called \"PAUSE\" in x86 which is used to give hint to a processor that a spin-lock is currently running on this CPU. The intel manual and other information available state that The processor uses this hint to avoid the memory order violation in most situations, which greatly improves processor performance. For this reason, it is recommended that a PAUSE instruction be placed in all spin

How to use lock in OpenMP?

徘徊边缘 提交于 2019-11-26 06:39:37
问题 I have two pieces of C++ code running on 2 different cores. Both of them write to the same file. How to use OpenMP and make sure there is no crash? 回答1: You want the OMP_SET_LOCK / OMP_UNSET_LOCK functions: https://computing.llnl.gov/tutorials/openMP/#OMP_SET_LOCK. Basically: omp_lock_t writelock; omp_init_lock(&writelock); #pragma omp parallel for for ( i = 0; i < x; i++ ) { // some stuff omp_set_lock(&writelock); // one thread at a time stuff omp_unset_lock(&writelock); // some stuff } omp