critical-section

Boost Asio if condition evaluated differently in static-lib and dll compilations, former resulting in exception in socket io cpp client lib

谁说胖子不能爱 提交于 2019-12-04 05:33:49
问题 Depending on how the socketio c++ library is compiled (static-lib or dll) for the following simple test code, the outcome is either a working executable or one that throws an exception. However, if the instantiation of the io_service is commented out, eg.: // boost::asio::io_service io_service; then the static-lib based version is also working without exception. It seems that there is some interference between the io_service instantiated in main() with the io_service located in the socketIO

How do I make a critical section with Boost?

余生颓废 提交于 2019-12-04 02:57:56
问题 For my cross-platform application I have started to use Boost, but I can't understand how I can implement code to reproduce behavior of Win32's critical section or .Net's lock . I want to write a method Foo that can be called from different threads to control write operations to shared fields. Recursive calls within the same thread should be allowed (Foo() -> Foo()). In C# this implementation is very simple: object _synch = new object(); void Foo() { lock (_synch) // one thread can't be lock

Replace critical section with SRW lock

Deadly 提交于 2019-12-03 16:32:44
问题 If the application is targeted on Windows Vista or later, could we replace all critical sections with SRW locks? Since critical section is mutually exclusive, for usage it is equivalent to SRW locks in exclusive mode, right? According to MSDN, SRW is optimized both for speed and space. Is there any drawback for doing this? I'm not sure how CS and SRW are implemented internally by Microsoft. Thanks! 回答1: See Joe Duffy's book "Concurrent Programming on Windows", pg 289. The short answer to your

“Pausing” A Thread With A Property

淺唱寂寞╮ 提交于 2019-12-03 10:07:13
问题 I have a TThread object and want to be able to start/stop the thread via a button on the main form of the program. I've been looking into ways to do this and so far I have the following ideas: Terminate and Free the thread when the user clicks stop and create a new one when they click start. Use sleep to delay the thread (I don't want to do this) Have a property that is a boolean to determine if the thread is paused or not. The code in the Execute will only happen if this boolean is false. I

Problems using EnterCriticalSection

╄→尐↘猪︶ㄣ 提交于 2019-12-03 08:46:15
问题 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; SharedVector(const SharedVector<T>& rhs) {} public: SharedVector(); explicit SharedVector(const CRITICAL_SECTION& CS); void PushBack(const T& value); void PopBack();

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

╄→гoц情女王★ 提交于 2019-12-03 07:24:39
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 Peterson's solution will work correctly on such architectures. I looked this up on Wikipedia and found this which appears to be the closest to an explanation : Most modern CPUs reorder memory accesses to improve execution efficiency. Such processors invariably give some way to force ordering

Replace critical section with SRW lock

耗尽温柔 提交于 2019-12-03 05:56:11
If the application is targeted on Windows Vista or later, could we replace all critical sections with SRW locks ? Since critical section is mutually exclusive, for usage it is equivalent to SRW locks in exclusive mode, right? According to MSDN, SRW is optimized both for speed and space. Is there any drawback for doing this? I'm not sure how CS and SRW are implemented internally by Microsoft. Thanks! Mark oskin See Joe Duffy's book "Concurrent Programming on Windows", pg 289. The short answer to your question is "almost". There are semantics with recursively acquired CRITICAL_SECTION's that are

pthreads : pthread_cond_signal() from within critical section

会有一股神秘感。 提交于 2019-12-03 04:27:10
问题 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); testCondition = true; pthread_cond_signal(&my_wait); pthread_mutex_unlock(&my_lock); Provided there are no other threads, would it make any difference if pthread_cond_signal(&my

Cost of mutex,critical section etc on Windows

a 夏天 提交于 2019-12-03 03:11:02
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-threads are accessing the mutexes(Just to eliminate the other benefit of Critical Sections) Added Info: OS

“Pausing” A Thread With A Property

耗尽温柔 提交于 2019-12-03 00:42:40
I have a TThread object and want to be able to start/stop the thread via a button on the main form of the program. I've been looking into ways to do this and so far I have the following ideas: Terminate and Free the thread when the user clicks stop and create a new one when they click start. Use sleep to delay the thread (I don't want to do this) Have a property that is a boolean to determine if the thread is paused or not. The code in the Execute will only happen if this boolean is false. I'm leaning towards #3. Would setting a boolean property on the TThread object from the main form be