mutex

Modifying data in threads

懵懂的女人 提交于 2020-01-03 03:01:11
问题 I have a class with the following structure: class Nginx_sender { private: std::vector<std::string> mMessagesBuffer; boost::mutex mMutex; void SendMessage(const std::string &msg) { mMutex.lock(); mMessagesBuffer.push_back(msg); mMutex.unlock(); std::cout << "Vector size: " << mMessagesBuffer.size() << std::endl; } void NewThreadFunction() { while(true) { mMutex.lock(); if (mMessagesBuffer.size() >= 1) std::cout << ">=1\n"; mMutex.unlock(); boost::this_thread::sleep(boost::posix_time:

Use mutex or not in a concurrent reading

两盒软妹~` 提交于 2020-01-02 19:23:11
问题 I am programming in C++ in Linux and I am using pthreads library. I am using mutex to protect some shared variables but I am not sure if in this specific case it is necessary the use of mutex. I have 3 threads. The shared variable is a string (global variable). Thread1 changes it's value and afterwards, thread2 and thread3 read it's value and store in another string. In this case, the string's value is only modified by one thread. Is still necessary the use of mutex to protect a shared

What is the difference between semaphore and mutex in implementation?

点点圈 提交于 2020-01-02 05:45:08
问题 I read that mutex and binary semaphore are different in only one aspect, in the case of mutex the locking thread has to unlock, but in semaphore the locking and unlocking thread can be different? Which one is more efficient? 回答1: Assuming you know the basic differences between a sempahore and mutex : For fast, simple synchronization, use a critical section. To synchronize threads across process boundaries, use mutexes. To synchronize access to limited resources, use a semaphore. Apart from

How to minimize the mutex locking for an object when only 1 thread mostly uses that object and the other thread(s) use it rarely?

≯℡__Kan透↙ 提交于 2020-01-01 12:32:32
问题 Scenario Suppose there are "Thread_Main" and "Thread_DB", with a shared SQLite database object. It's guaranteed that, "Thread_main" seldom uses SQLite object for reading (i.e. SELECT() ) "Thread_DB" uses the SQLite object most of the time for various INSERT , UPDATE , DELETE operations To avoid data races and UB, SQLite should be compiled with SQLITE_THREADSAFE=1 (default) option. That means, before every operation, an internal mutex will be locked, so that DB is not writing when reading and

Mutex are needed to protect the Condition Variables

你。 提交于 2020-01-01 06:43:48
问题 As it is said that Mutex are needed to protect the Condition Variables. Is the reference here to the actual condition variable declared as pthread_cond_t OR A normal shared variable count whose values decide the signaling and wait. ? 回答1: is the reference here to the actual condition variable declared as pthread_cond_t or a normal shared variable count whose values decide the signaling and wait? The reference is to both. The mutex makes it so that the shared variable ( count in your question)

How to correctly destroy pthread mutex

若如初见. 提交于 2020-01-01 05:40:08
问题 How exactly i can destroy a pthread mutex variable ? Here is what i want to do. I want to have objects (structure variables) cached , which are looked up by key. I want to have minimum granularity of locks here. So i want to have a lock for each object probably embedded in the structure so that i can have object level locking. Now the problem is how to safely destroy these objects ? Looks like first step is to remove the object from the lookup table so that the object is not accessible in

Get a list of mutex?

我们两清 提交于 2019-12-31 13:14:21
问题 A program creates a mutex as part of its start-up. I don't know the format of this mutex so I wondered if there is a way to get a list of all non-abandoned mutex, open the program, get a new list and see if I can find the mutex by removing all duplicate entries. Is there a way to get this list? 回答1: If you're on Windows, WinObj can show you named mutexes. Or you can use Process Explorer to find out which objects a specific process has open. 回答2: If you have WinObj.exe it is likely that you

Get a list of mutex?

元气小坏坏 提交于 2019-12-31 13:12:03
问题 A program creates a mutex as part of its start-up. I don't know the format of this mutex so I wondered if there is a way to get a list of all non-abandoned mutex, open the program, get a new list and see if I can find the mutex by removing all duplicate entries. Is there a way to get this list? 回答1: If you're on Windows, WinObj can show you named mutexes. Or you can use Process Explorer to find out which objects a specific process has open. 回答2: If you have WinObj.exe it is likely that you

Mutex not releasing

时光总嘲笑我的痴心妄想 提交于 2019-12-31 03:17:28
问题 My c# WinForm solution contains several projects including an Admin project containing frmAdmin and a User project containing frmUser. A third project contains frmTimer that has a timer that periodically launches frmUser. I want frmTimer to not launch frmUser when frmAdmin is open. I'm using a named mutex to tell frmTimer if frmAdmin is open; however, the mutex appears not to be released after frmAdmin is closed. The mutex is created in frmAdmin with code like this: public partial class

Using Named Mutex

旧时模样 提交于 2019-12-31 01:54:05
问题 I have two instances running of same Windows Service. They check the health of each other and report if any issue is found. I have a critical job that needs to be performed so I am running it with a fail-over approach, it runs in Master, and if Master is not responding it runs in slave. This job needs to communicate over a specific serial port, I am trying to use Mutex to check for race condition. I dont have access to production, so before deploying I want to make sure my approach is fine.