critical-section

Is SetEvent atomic?

Is it safe to have 2 or more threads call the Win32 API's SetEvent on the same event handler not being protected by a critical section? ...

What is wrong with this tiny piece of mutex code?

// A Mutex allows threads mutually exclusive access to a resource. //----------------------------------------------------------------------- class Mutex { private: CRITICAL_SECTION m_mutex; public: Mutex() { InitializeCriticalSection(&m_mutex); } ~Mutex() { DeleteCriticalSection(&m_mutex); } void acquire() { Enter...

Problems using EnterCriticalSection

I need to work with array from several threads, so I use CRITICAL SECTION to give it an exclusive access to the data. Here is my template: #include "stdafx.h" #ifndef SHAREDVECTOR_H #define SHAREDVECTOR_H #include <vector> #include <windows.h> template<class T> class SharedVector { std::vector<T> vect; CRITICAL_SECTION cs; ...

Critical section - to be or not to be?

I`m writing a chat using WinSock2 and WinAPI functions. And I have a little trouble. I store the std::vector of client connections on server. When new client connects, new thread starts and all work with the client is done in this new thread. I do not use classes (I know it is not very good) so this list of connections is just defined as...

Avoid Re-initialization of Critical Section

Exact Duplicate Initialize Critical Section only once for a process I have a dll that creates a global critical section, initializes and use it. Now a third party application is using / loading the dll more than once which leads to a heap corruption. The appverifier warns me with a --> VERIFIER STOP 00000211: pid 0x1470: Critic...

Is the memory not reclaimed for Delphi apps running on Windows Server 2008 (sp1) ?

We have a D2007 application whose memory footprint grows steadily when running on Windows Server 2008 (x64, sp1). It behaves normally on Windows Server 2003 (x32 or x64), XP, etc... where it goes up and down as expected. We have tried with the included Memory Manager or the latest FastMM4 4.92 with the same results. Has anyone tried t...

Critical Sections leaking memory on Vista/Win2008?

It seems that using Critical Sections quite a bit in Vista/Windows Server 2008 leads to the OS not fully regaining the memory. We found this problem with a Delphi application and it is clearly because of using the CS API. (see this SO question) Has anyone else seen it with applications developed with other languages (C++, ...)? The s...

Is Critical Section always faster ?

I was debugging a multi-threaded application and found the internal structure of CRITICAL_SECTION. I found data member LockSemaphore of CRITICAL_SECTION an interesting one. It looks like LockSemaphore is an auto-reset event (not a semaphore as the name suggests) and operating system creates this event silently when first time a thread ...

App Verifier reporting "Thread cannot own a critical section."

So App Verifier is throwing this exception. From what I gather, the text of this message is a little misleading. The problem appears to be that the the critical section was created by a thread that is being destroyed before the critical section is destroyed. It's a relatively simple fix but does anyone know what the ramifications ar...

Win32 Read/Write Lock Using Only Critical Sections

I have to implement a read/write lock in C++ using the Win32 api as part of a project at work. All of the existing solutions use kernel objects (semaphores and mutexes) that require a context switch during execution. This is far too slow for my application. I would like implement one using only critical sections, if possible. The loc...

Starvation of threads with Windows 2003 SP2

To our great surprise we found recently this. With SP1 for Windows 2003 Microsoft changed a way critical sections behave. Earlier threads wanting to access them were served in FIFO manner. Right now they are served in pure "random" way. In our case we had something like this: // I now it's kind of ugly design but works void Class:RunIn...

N processes and M types of processes - enter and exit cs

i was asked to write: enter function and exit function for the following case: there are N processes and M types of processes (N>>M) tere is a critical section in which all processes with the same type can enter. for example: if type A is in cs, type B cannot enter cs. but all processes with type A can enter. i can use only mutex and "...

What is the relation ship between CRITICAL_SECTION and CCriticalSection

What is the relation ship between CRITICAL_SECTION and CCriticalSection. is CCriticalSection a wrapper of CRITICAL_SECTION? BTW: I think the following code is meanless because the cs is not Global, it initial every times before lock() so it can't lock the XXX , is it ? int func { CCriticalSection cs; cs.Lock(); XXX cs.Unlock(); } Ma...

Multiple producer/consumer and critical section code problem

I am attempting a multiple producer/consumer problem in C, but its not working as expected. The following is some pseudo code to represent my implementation. Thread thread1; Thread thread2; Thread thread3; Data data1; Mutex data1_mutex; Semaphore data1_empty; Semaphore data1_fill; Data data2; Mutex data2_mutex; Semaphore data2_empty; ...

Disable Hardware & Software Interrupts

Hello! Is it possible to disable all interrupts with a ASM/C/C++ program to get full control about the processor? If yes -> how? If not -> how do "atomic" operation system calls work (for example entering a critical section)? Sorry for my English! Thanks for your help! ...

Question on critical section algorithm

The Operating System Concepts 6th edition present one trival algorithm to implementate ciritical section. do{ while (turn != i); critical section trun = j; remainder section } while(1); Note,Pi is the process with identifier i,Pj is the process with identifier j.To simlify the question,the book limit the i,j to 0 and 1,th...

pthreads : pthread_cond_signal() from within critical section

I have the following piece of code in thread A, which blocks using pthread_cond_wait() pthread_mutex_lock(&my_lock); if ( false == testCondition ) pthread_cond_wait(&my_wait,&my_lock); pthread_mutex_unlock(&my_lock); I have the following piece of code in thread B, which signals thread A pthread_mutex_lock(&my_lock...

CriticalSection

hey, i'm not sure about something. when i use critical_section/mutex/semaphor in c++ for example , how does the busy_wait problem being prevented ? what i mean is when a thread reaches a critical section and the critical section is occupied by other thread, what prevents the thread from wasting cycle time and wait for nothing ? for e...

How to implement a critical section in CUDA?

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 (atomi...

Do I need to lock object when reading from it?

I am writing a program where there is an object shared by multiple threads: a. multiple write threads write to the object (all running the same function) b. a read thread which accesses the object every 5 seconds c. a read thread which accesses the object there is a user request It is obviously necessary to lock the object when writing ...