mutex

Do I need a mutex on a vector of pointers?

廉价感情. 提交于 2019-12-02 16:08:25
问题 Here is a simplified version of my situation: void AppendToVector(std::vector<int>* my_vector) { for (int i = 0; i < 100; i++) { my_vector->push_back(i); } } void CreateVectors(const int num_threads) { std::vector<std::vector<int>* > my_vector_of_pointers(10); ThreadPool pool(num_threads); for (for int i = 0; i < 10; i++) { my_vector_of_pointers[i] = new std::vector<int>(); pool.AddTask(AppendToVector, &my_vector_of_pointers[i]); } } My question is whether I need to put a mutex lock in

Implementing mutexes for file writes

本秂侑毒 提交于 2019-12-02 14:53:16
I am trying to use mutexes to avoid multiple writes to the same thread inC/Cpp. Below is the flow of my program. I am confused as to where to include my lock and unlock code. main() { spawn a worker thread } worker_thread() { read the input file name read some content write the content to the given file name } Most of the implementation that I see, seem to have something like this: main() { pthread_mutex_init(&myMutex;,0); *spawn a worker thread* pthread_join(thread1, 0); pthread_mutex_destroy(&myMutex;); } worker_thread() { read the input file name read some content write the content to the

std::mutex vs std::recursive_mutex as class member

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 14:17:30
I have seen some people hate on recursive_mutex : http://www.zaval.org/resources/library/butenhof1.html But when thinking about how to implement a class that is thread safe (mutex protected), it seems to me excruciatingly hard to prove that every method that should be mutex protected is mutex protected and that mutex is locked at most once. So for object oriented design, should std::recursive_mutex be default and std::mutex considered as an performance optimization in general case unless it is used only in one place (to protect only one resource)? To make things clear, I'm talking about one

How to use mutex

痴心易碎 提交于 2019-12-02 13:30:58
Where should i put the lock and unlock mutex in order for the threads to print alternatively? Thanks:D Implement a program that creates two threads. The threads will print their ID (pthread_self) 10 times and then stop. Insure that the printed IDs alternate always (ie A, B, A, B, ...) #include <stdio.h> #include <pthread.h> #define N 2 pthread_mutex_t mtx; void* func (void* arg) { int i=0; int f=1; for(i=0; i<10; i++) { printf("%d%s%d\n",f ,": ", (int)pthread_self()); f++; } return NULL; } int main() { int i; pthread_t thr[N]; pthread_mutex_init(&mtx, NULL); for(i=0; i<N; i++) { pthread_create

Properly deleting a singleton

淺唱寂寞╮ 提交于 2019-12-02 11:52:13
I have the following code: MyClass.h: static MyMutex instanceMutex; static MyClass* getInstance(); static void deleteInstance(); MyClass.c: MyMutex MyClass::instanceMutex; MyClass* MyClass::getInstance() { if (theInstance == 0) { instanceMutex.acquire(); if (theInstance == 0) { theInstance = new MyClass(); } instanceMutex.release(); } return theInstance; } void MyClass::deleteInstance() { if (theInstance != 0) { instanceMutex.acquire(); if (theInstance != 0) { theInstance->finalize(); delete theInstance; theInstance = 0; } instanceMutex.release(); } return; } I have 2 questions on this: Is the

How to lock (Mutex) in NodeJS?

孤街浪徒 提交于 2019-12-02 11:31:31
There are external resources (accessing available inventories through an API) that can only be accessed one thread at a time. My problems are: NodeJS server handles requests concurrently, we might have multiple requests at the same time trying to reserve inventories. If I hit the inventory API concurrently, then it will return duplicate available inventories Therefore, I need to make sure that I am hitting the inventory API one thread at a time There is no way for me to change the inventory API (legacy), therefore I must find a way to synchronize my nodejs server. Note: There is only one

Properly deleting a singleton

Deadly 提交于 2019-12-02 10:59:30
问题 I have the following code: MyClass.h: static MyMutex instanceMutex; static MyClass* getInstance(); static void deleteInstance(); MyClass.c: MyMutex MyClass::instanceMutex; MyClass* MyClass::getInstance() { if (theInstance == 0) { instanceMutex.acquire(); if (theInstance == 0) { theInstance = new MyClass(); } instanceMutex.release(); } return theInstance; } void MyClass::deleteInstance() { if (theInstance != 0) { instanceMutex.acquire(); if (theInstance != 0) { theInstance->finalize(); delete

Do I need a mutex on a vector of pointers?

大兔子大兔子 提交于 2019-12-02 09:46:02
Here is a simplified version of my situation: void AppendToVector(std::vector<int>* my_vector) { for (int i = 0; i < 100; i++) { my_vector->push_back(i); } } void CreateVectors(const int num_threads) { std::vector<std::vector<int>* > my_vector_of_pointers(10); ThreadPool pool(num_threads); for (for int i = 0; i < 10; i++) { my_vector_of_pointers[i] = new std::vector<int>(); pool.AddTask(AppendToVector, &my_vector_of_pointers[i]); } } My question is whether I need to put a mutex lock in AppendToVector when running this with multiple threads? My intuition tells me I do not have to because there

Concurrent File write between processes

独自空忆成欢 提交于 2019-12-02 09:07:11
I need to write log data into a single file from different processes. I am using Windows Mutex which needs Common Language Runtime support for it. Mutex^ m = gcnew Mutex( false,"MyMutex" ); m->WaitOne(); //... File Open and Write .. m->ReleaseMutex() Do I really need to change from C++ to C++/CLI for synchronization? It is ok if the atomic is not used. But I need to know whether using this Mutex will slow down the performance compared to local mutex. Adding CLR support to your C++ application just to get the Mutex class is overkill. There are several options available to you to synchronize

In dotnet core how can I ensure only one copy of my application is running?

柔情痞子 提交于 2019-12-02 06:45:49
In the past I have done something like this private static bool AlreadyRunning() { var processes = Process.GetProcesses(); var currentProc = Process.GetCurrentProcess(); logger.Info($"Current proccess: {currentProc.ProcessName}"); foreach (var process in processes) { if (currentProc.ProcessName == process.ProcessName && currentProc.Id != process.Id) { logger.Info($"Another instance of this process is already running: {process.Id}"); return true; } } return false; } Which has worked well. In the new dotnet core world everything has a process name of dotnet so I can only run one dotnet app at a