mutex

System-wide global variable / semaphore / mutex in C++/Linux?

Deadly 提交于 2019-12-12 10:37:32
问题 Is it possible to create a system-wide global variable / semaphore / mutex in C++ on Linux? Here's the reason: I've got a system that often runs multiple copies of the same software on unrelated data. It's common to have 4 jobs, each running the same software. The software has a small section where it creates a huge graph that takes a lot of memory; outside that section memory usage is moderate. It so happens sometimes that 2 jobs simultaneously hit the same memory-hungry section and the

How to use named mutex at linux?

99封情书 提交于 2019-12-12 10:24:33
问题 I'd like to use named mutex at linux for multi process. I searched at google and find some APIs. But, it's hard to break out the coding. I found two apis and it looks set name and be shared between processes. pthread_mutexattr_setname_np pthread_mutexattr_setpshared(&mtx_attr, PTHREAD_PROCESS_SHARED); Could somebody give me hint or example? 来源: https://stackoverflow.com/questions/46313748/how-to-use-named-mutex-at-linux

Delete an object securely from a multi-threaded program

孤者浪人 提交于 2019-12-12 09:27:53
问题 DISCLAIMER: neither Boost, nor C++11 allowed. I've a program, in which I create an instance of Foo and I operate with it in a number of threads. Then I want to delete it securely so those threads do not fall into a segmentation fault. I've added a mutex member to Foo and lock it each time a thread function runs. In order different threads do not conflict with each other. class Foo { public: pthread_mutex_t mutex; }; void* thread ( void* fooPtr ) { Foo* fooPointer = (Foo*) fooPtr; while ( 1 )

C# mutex issue to prevent second instance

天大地大妈咪最大 提交于 2019-12-12 05:57:46
问题 I got an interesting problem (C#/WPF application). I am using this code to prevent second instance of my application from running. Mutex _mutex; string mutexName = "Global\\{SOME_GUID}"; try { _mutex = new Mutex(false, mutexName); } catch (Exception) { //Possible second instance, do something here. } if (_mutex.WaitOne(0, false)) { base.OnStartup(e); } else { //Do something here to close the second instance } If I put the code directly in the main exe under OnStartup method it works. However

Window frame hang due to misuse of thread

假如想象 提交于 2019-12-12 05:57:09
问题 I am getting a database table value in wxListCtrl, (data can be large so I am using wxThread) Child thread send row to Main thread and main thread fill it in wxListCtrl, everything is going good but when I m trying to close the frame , it is giving me unexpected result. at the close button , I am invoking a new frame, sometime it opens and some time not, my code is:- if(thread_object_holder->IsAlive()) { wxPuts(wxT("live")); thread_object_holder->Pause(); wxMessageDialog *msg = new

QMutex needed to read variable

▼魔方 西西 提交于 2019-12-12 04:56:27
问题 I command my threads via public accessible member variables that are usually protected by an according mutex. My question is this: If a single variable is protected by a mutex during write access should it also be protected during read access or can I simply read it? Example: A thread checks if something special should be done ( doSpecial is written in another thread) // some code if (doSpecial) { // code } // some code Should this read access be protected by a mutex? 回答1: Yes, if the

syncronizing 2 threads c++ linux

ぃ、小莉子 提交于 2019-12-12 04:49:27
问题 i have such code #include <iostream> #include <thread> #include <mutex> #include <iostream> #include <unistd.h> using namespace std; bool isRunning; mutex locker; void threadFunc(int num) { while(isRunning) { locker.lock(); cout << num << endl; locker.unlock(); sleep(1); } } int main(int argc, char *argv[]) { isRunning = true; thread thr1(threadFunc,1); thread thr2(threadFunc,2); cout << "Hello World!" << endl; thr1.join(); thr2.join(); return 0; } when running this code i'm waiting to get

Pthread Synchronization: Back & Forth Reading of Two Text Files

∥☆過路亽.° 提交于 2019-12-12 04:41:29
问题 I'm writing a program that creates two threads. Each thread is responsible for reading through one text file, with one char on each line. The first is formatted like: h 0 h 0 ... The second is formatted like: 0 i 0 i 0 i Sometimes there can be multiple letters after each other, or multiple zeros after each other. However, the one certainty is that if there is a letter on one line of one file, the corresponding line of the second file will have a 0, and vice versa. The threads are supposed to

How to consume a system mutex shared by C# and Java programs?

断了今生、忘了曾经 提交于 2019-12-12 04:35:00
问题 I am creating a framework whereby programs written in different languages can share an event structure based on named system mutexes in Windows. I've got it working perfectly in C#. Multiple apps can listen for and signal various events, and other apps can catch them, react, etc. The idea is that programs written in any of the various languages we use where I work (such as Java and others) can fit themselves into this system as long as they play within the rules of this event structure. How

Only one thread gets created despite the attempt to create two

梦想与她 提交于 2019-12-12 03:22:34
问题 #include <pthread.h> #include <stdio.h> #include <unistd.h> #include <vector> #include <string> #include <iostream> FILE* fp; pthread_mutex_t demoMutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t conditionVariable = PTHREAD_COND_INITIALIZER; unsigned int condition = 0; struct serverInfo { unsigned int serverId; pthread_t threadId; std::vector<std::string> queue; }; std::vector<serverInfo> serverInfoVector; void* printHello(void* threadId) { pthread_t* my_tid = (pthread_t*)threadId; pthread