critical-section

InitializeCriticalSectionAndSpinCount optimal SpinCount (user mode)

安稳与你 提交于 2019-12-07 18:21:56
问题 I don't quite understand the documentation for InitializeCriticalSectionAndSpinCount: http://msdn.microsoft.com/en-us/library/windows/desktop/ms683476(v=vs.85).aspx It says "You can improve performance significantly by choosing a small spin count ..." However, since waiting on a spinner is faster than waiting for an object, doesn't it make sense to have the SpinCount as high as possible? What am I missing? Thanks. (I am using it inside a C DLL used by a multi-threaded application) Here is the

Do I need to use volatile keyword for memory access in critical section?

若如初见. 提交于 2019-12-07 14:21:20
问题 I am writing code for a single processor 32 bit microcontroller using gcc. I need to consume time-stamped objects from a linked list. Another part of the code which could be asynchronous (maybe in an ISR) adds them to the list. The critical section is implemented by turning interrupts off and using the barrier() function. I'm confused where gcc optimization could break my code by cacheing pointers to the list items (next most recent item to remove, list head, or free list). I dont want

Unable to enter critical section

房东的猫 提交于 2019-12-07 10:34:39
问题 Why is it imposible to enter critical section without Sleep(1)? type TMyThread = class(TThread) public procedure Execute; override; end; var T: TMyThread; c: TRTLCriticalSection; implementation procedure TForm1.FormCreate(Sender: TObject); begin InitializeCriticalSection(c); T := TMyThread.Create(false); end; procedure TMyThread.Execute; begin repeat EnterCriticalSection(c); Sleep(100); LeaveCriticalSection(c); sleep(1); // can't enter from another thread without it until false; end;

Critical Sections leaking memory on Vista/Win2008?

帅比萌擦擦* 提交于 2019-12-07 04:56:53
问题 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 sample code was just initialzing 10000000 CS, then deleting them. This works fine in XP/Win2003 but does not release all the peak memory in Vista/Win2008 until the

Scenario: Global variables in DLL which is used by Multi-threaded Application

僤鯓⒐⒋嵵緔 提交于 2019-12-07 04:10:31
问题 Few months back, I had come across this interesting scenario asked by a guy (on orkut). Though, I've come up with a "non-portable" solution to this problem (have tested it with small code), but still would like to know what you guys have to say and suggest. Suppose, I created a DLL, exporting some functionalities, written in C++, for single threaded client . This DLL declares lots of global variables, some maybe const variables (read-only) and others are modifiable. Anyway, later things

Windows 10 specific crash on call LeaveCriticalSection

假如想象 提交于 2019-12-06 12:44:55
问题 I stucked into a problem with threads syncronization and critical sections on Windows 10. Application will crash in this case: Application has two threads. Thread 1 calls EnterCriticalSection with object m_CS Thread 2 then attempts to enter the same critical section Thread 1 terminates Thread 2 using TerminateThread Thread 1 calls LeaveCriticalSection In previous Windows versions which I was able to test (7, 8, 8.1) this works properly. Thread 2 terminates, and Thread 1 leaves the critical

c++ OpenMP critical: “one-way” locking?

核能气质少年 提交于 2019-12-06 07:58:21
问题 Consider the following serial function. When I parallelize my code, every thread will call this function from within the parallel region (not shown). I am trying to make this threadsafe and efficient (fast). float get_stored_value__or__calculate_if_does_not_yet_exist( int A ) { static std::map<int, float> my_map; std::map::iterator it_find = my_map.find(A); //many threads do this often. bool found_A = it_find != my_map.end(); if (found_A) { return it_find->second; } else { float result_for_A

InitializeCriticalSectionAndSpinCount optimal SpinCount (user mode)

蓝咒 提交于 2019-12-06 04:35:52
I don't quite understand the documentation for InitializeCriticalSectionAndSpinCount: http://msdn.microsoft.com/en-us/library/windows/desktop/ms683476(v=vs.85).aspx It says "You can improve performance significantly by choosing a small spin count ..." However, since waiting on a spinner is faster than waiting for an object, doesn't it make sense to have the SpinCount as high as possible? What am I missing? Thanks. (I am using it inside a C DLL used by a multi-threaded application) Here is the code for the critical section, called constantly by a large number of threads: int g_slots[256] = {0};

Multithreading and Critical Sections Use - C++

血红的双手。 提交于 2019-12-06 03:15:45
I'm a little confused as to the proper use of critical sections in multithreaded applications. In my application there are several objects (some circular buffers and a serial port object) that are shared among threads. Should access of these objects always be placed within critical sections, or only at certain times? I suspect only at certain times because when I attempted to wrap each use with an EnterCriticalSection / LeaveCriticalSection I ran into what seemed to be a deadlock condition. Any insight you may have would be appreciated. Thanks. If you share a resource across threads, and some

C++: Concurrency and destructors

核能气质少年 提交于 2019-12-05 13:55:46
Suppose you have an object which can be accesed by many threads. A critical section is used to protect the sensitive areas. But what about the destructor? Even if I enter a critical section as soon as I enter the destructor, once the destructor has been called, is the object already invalidated? My train of thought: Say I enter the destructor, and I have to wait on the critical section because some other thread is still using it. Once he is done, I can finish destroying the object. Does this make sense? In general, you should not destroy an object until you know that no other thread is using