mutex

Removing Windows library dependencies

浪子不回头ぞ 提交于 2020-01-06 06:46:49
问题 I have a couple of classes here that I would like to remove window library dependencies for portability reasons. One for blocking processes and the other for blocking threads. Both of these classes compile & runs fine as is... As for the BlockProcess class it is currently using a HANDLE for a mutex and using function calls such as: { CreateMutex() , CloseHandle() & GetLastError() }. As for the BlockThread class it uses a pointer to a CRITICAL_SECTION calling functions such as: {

C++: Control order of thread execution with mutex's and conditional variables

一笑奈何 提交于 2020-01-05 23:26:53
问题 This question is a follow up for this question. I want threads to perform some work and pass handle to the next thread in order. When trying to execute the following code, I get Unhandled exception at 0x0F7C1F5F (msvcp120d.dll) in ConsoleApplication9.exe: 0xC0000005 : Access violation reading location 0x00000004. #include "stdafx.h" #include <iostream> #include <thread> #include <mutex> #include <chrono> #include <condition_variable> std::mutex* m_pMutexs; std::condition_variable* m_pCVs; int

Issue with Mutex getting lock before it was released and how to distribute locks equally?

丶灬走出姿态 提交于 2020-01-05 07:04:51
问题 The method GetItemSearchResponse will be called by multiple console application. But I wanted to call this method one by one. So I applied mutex. So that this method will be locked for other threads. public class AWSItemSearchClient : IDisposable { // the name of the global mutex; private const string MutexName = "FAA9569-7DFE-4D6D-874D-19123FB16CBC-8739827-[SystemSpecicString]"; private Mutex _globalMutex; private bool _owned = false; //This method call should be synchronized public

synchronizing access to allocated memory

我的梦境 提交于 2020-01-05 02:33:27
问题 Is there a way to synchronize access to each element in an allocated memory. For example, if I allocate memory using the following code int* counters = new int[10]; is there a way to synchronize modification of each counter separately (being able to modify counters[0], counters[1]...counters[9] at the same time) so that modification of, let's say, counters[0] won't block counters[9] until the lock is released to update counters[9] and the other counters while a thread is updating a specific

storing mutexes in a vector/deque c++

∥☆過路亽.° 提交于 2020-01-05 02:28:06
问题 I would like to store a variable number of mutexes in a container like vector or deque. In one of the use cases, I need to reliably and deadlock-free lock all of the mutexes. I would also like to have exception safety guarantee, that if an exception is thrown, all of the mutexes are as if no locking occured. I am trying to do something like: std::vector<std::mutex> locks_(n); std::vector<std::lock_guard<std::mutex> > guards(n); for(int i = 0; i < n; i++) { std::lock_guard<std::mutex> guard

Logging in multi threaded application, Using mutex in a constructor

寵の児 提交于 2020-01-04 14:07:14
问题 I have been writing a C++ method entry/exit logger, based on RAII. The usage is something like this: void Class::Method() { METHOD_NAME( "Class::Method" ); // I know: I could use __FUNCTION__ instead ;<) … } Logger class: #define METHOD_NAME(name) TraceLogger _traceLog(name); TraceLogger::TraceLogger( const std::string& theContext ) { <lock mutex here> // Trace logging code here } TraceLogger::~TraceLogger() { <lock mutex here> // Trace logging code here } The problem is that the code is not

Logging in multi threaded application, Using mutex in a constructor

萝らか妹 提交于 2020-01-04 14:06:31
问题 I have been writing a C++ method entry/exit logger, based on RAII. The usage is something like this: void Class::Method() { METHOD_NAME( "Class::Method" ); // I know: I could use __FUNCTION__ instead ;<) … } Logger class: #define METHOD_NAME(name) TraceLogger _traceLog(name); TraceLogger::TraceLogger( const std::string& theContext ) { <lock mutex here> // Trace logging code here } TraceLogger::~TraceLogger() { <lock mutex here> // Trace logging code here } The problem is that the code is not

Proper use of MPI_THREAD_SERIALIZED with pthreads

笑着哭i 提交于 2020-01-04 09:06:52
问题 After reading some MPI specs I'm lead to understand that, when initializing with MPI_THREAD_SERIALIZED, a program must ensure that MPI_Send/Recv calls that occur in separate threads must not overlap. In other words, you need a mutex to protect MPI calls. Consider this situation: Mutex mpi_lock = MUTEX_INITIALIZER; void thread1_function(){ while(true){ /* things happen */ lock(mpi_lock); MPI_Send(/* some message */); unlock(mpi_lock); /* eventually break out of loop */ } } void thread2

Will the non-lexical lifetime borrow checker release locks prematurely?

痞子三分冷 提交于 2020-01-04 06:21:08
问题 I've read What are non-lexical lifetimes?. With the non-lexical borrow checker, the following code compiles: fn main() { let mut scores = vec![1, 2, 3]; let score = &scores[0]; // borrows `scores`, but never used // its lifetime can end here scores.push(4); // borrows `scores` mutably, and succeeds } It seems reasonable in the case above, but when it comes to a mutex lock, we don't want it to be released prematurely. In the following code, I would like to lock a shared structure first and

Different Between Static Mutext and Not Static Mutex

六月ゝ 毕业季﹏ 提交于 2020-01-04 05:52:07
问题 I have a code in .cpp namespapce A { namespace { static CMutex initMutex; } void init() { //code here } void uninit() { //code here } } What is the different if I remove the static in the mutex and if there is a static? And what is the use of the static? Thanks! 回答1: If mutex is static and if it would have been in the header and that header included in 2 cpp files(2 translational units), the lock applied by the code in first file will not be seen by the second file which is dangerous. This is