critical-section

Unable to enter critical section

妖精的绣舞 提交于 2019-12-05 12:12:10
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; procedure TForm1.Button1Click(Sender: TObject); begin EnterCriticalSection(c); Caption := 'entered';

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

别来无恙 提交于 2019-12-05 11:20:26
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 changed and now I want the same DLL to work with multi-threaded application (without modifying the DLL);

Critical Sections and return values in C++

夙愿已清 提交于 2019-12-05 04:40:50
In attempting to create a thread-safe container class from scratch, I've run into the problem of returning values from access methods. For example in Windows: myNode getSomeData( ) { EnterCriticalSection(& myCritSec); myNode retobj; // fill retobj with data from structure LeaveCriticalSection(& myCritSec); return retobj; } Now I suppose that this type of method is not at all thread-safe because after the code releases the critical section another thread is able to come along and immediately overwrite retobj before the first thread returns. So what is an elegant way to return retobj to the

using dispatch_sync as a mutex lock

烂漫一生 提交于 2019-12-05 00:40:33
问题 Here is what I need to do. I hope dispatch_sync would be the best way to do it using GCD I have a certain piece of critical section code that is placed in the applicationDidBecomeActive callback in Appdelegate.. I am wrapping up that method inside a dispatch_sync call so that it gets called only once no matter how many times applicationDidBecomeActive is called - (void)applicationDidBecomeActive:(UIApplication *)application{ dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY

.crt section? What does this warning mean?

北城以北 提交于 2019-12-04 19:23:21
问题 I've got this warning recently (VC++ 2010) warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators I'm assuming this is the Critical Section. It's been a while since my Operating Systems course, so I can't really figure out what this means. If I remember right, the Critical Section works with shared resources. So how is this warning related and what does it mean exactly? 回答1: No, CRT = C Run Time. It is support library that any program needs to get the

Windows 10 specific crash on call LeaveCriticalSection

那年仲夏 提交于 2019-12-04 19:10:35
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 section without exception. On Windows 10, when Thread 1 leaves the critical section, application crashes

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

旧巷老猫 提交于 2019-12-04 14:58:56
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 = calculate_value(A); //should only be done once, really. my_map[A] = result_for_A; return result_for_A

Will Peterson's solution work correctly on modern CPU architectures? [closed]

痞子三分冷 提交于 2019-12-04 11:52:15
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . I am studying operating systems from Operating System Concepts by Silberschatz, Galvin, and Gagne. On page 229, the book states this about Petersons Solution : Because of the way modern computer architectures perform basic machine language instructions, such as load and store, there are no guarantees that

Is it safe to access VT data from the other thread?

允我心安 提交于 2019-12-04 11:06:01
Is it safe to change VirtualTreeView data from the secondary thread ? And if yes, should I use critical sections (or even Synchronize method) ? I'm afraid that when I'll be writing to the VT's data record from the other thread, main thread invokes its repaint meanwhile and this refresh will cause reading of the same record at one time. I would supplement I'm using only 2 threads in the application. Something like ... type PSomeRecord = ^TSomeRecord; TSomeRecord = record SomeString: string; SomeInteger: integer; SomeBoolean: boolean; end; ... var FCriticalSection: TRTLCriticalSection; // global

Cost of mutex,critical section etc on Windows

给你一囗甜甜゛ 提交于 2019-12-04 09:32:22
问题 I read somewhere that the overhead of a mutex is not that much, because the context switching only happens in case of contention. Also known Futexes in Linux. Does the same thing hold good in Windows? Is Critical Section a more apt map to mutexes in Linux. From what i gathered, Critical Sections provide better optimal performance compared to Mutex, is this true for every case? Is there a corner case where mutexes are faster than critical section in Windows. Assume only a single process