mutex

Android NDK problem pthread_mutex_unlock issue

自古美人都是妖i 提交于 2019-12-12 01:54:50
问题 I'm having an issue with pthread_mutex_unlock and my native activity NDK app. I've added logging messages to each mutex initialization, lock, lock success and unlock. My program is getting deadlocked because the mutex unlock is telling me that the thread trying to unlock the mutex did not lock it. My logging says otherwise. So I'm wondering if there is something I'm doing / not doing that could be causing this. The number in () is the mutex_t* followed by the code line number and thread id

Creating named Mutex in c# exe and Accessing it a dll in c++

Deadly 提交于 2019-12-12 01:35:10
问题 I have two exe's one in C# and other is a vc++ exe . Both of these exe need to access a file. So I am planning to create a named mutex in c#. vc++ how can i access this named mutex. Can any one give me sample codes for this 回答1: Something like this in the C++ code: HANDLE hMutex = CreateMutex(NULL, FALSE, name); if (hMutex == NULL) { // Handle failure. } If you need to know if the mutex already existed, check for hMutex != null && GetLastError() == ERROR_ALREADY_EXISTS . The default ACL you

Control each thread by it's own condition variable c++

假如想象 提交于 2019-12-12 01:07:54
问题 I am trying to create a program where there is one master thread and multiple worker threads. Worker threads will register themselves in a shared queue and will wait for a signal from master thread to move on. Master thread will check front of queue and will signal that thread by using it's own specific condition variable, to move on. Here is the pseudo-code that i have right now, struct condition{ pthread_cond_t cond_var; pthread_mutex_t lock; }; queue<condition> queue; pthread_mutex_t lock;

alternative to mutex lock/unlock when accessing class data

左心房为你撑大大i 提交于 2019-12-11 23:12:50
问题 I am trying to make my_class thread-safe like so. class my_class { const std::vector<double>& get_data() const { //lock so that cannot get_data() while setting data lock l(m_mutex); return m_data; } void run() { vector<double> tmp; //some calculations on tmp. { //lock so that cannot get_data() while setting m_data lock l(m_mutex); m_data = tmp; //set the data } } private: std::vector<double> m_data; mutex m_mutex; my_class(); //non-copyable } run() and get_data() may be called by different

Slow communication using shared memory between user mode and kernel

不打扰是莪最后的温柔 提交于 2019-12-11 19:35:37
问题 I am running a thread in the Windows kernel communicating with an application over shared memory. Everything is working fine except the communication is slow due to a Sleep loop. I have been investigating spin locks, mutexes and interlocked but can't really figure this one out. I have also considered Windows events but don't know about the performance of that one. Please advice on what would be a faster solution keeping the communication over shared memory possibly suggesting Windows events.

C/C++ - Single semaphore of type sem_t to print numbers in order

蹲街弑〆低调 提交于 2019-12-11 19:33:59
问题 Problem: Let's say we have n threads where each thread receives a random unique number between 1 and n. And we want the threads to print the numbers in sorted order. Trivial Solution (using n semaphore/mutex): We can use n mutex locks (or similarly semaphores) where thread i waits to acquire mutex lock number i and unlocks number i + 1. Also, thread 1 has no wait. However, I'm wondering if it's possible to simulate a similar logic using a single semaphore (of type sem_t) to implement the

Scoped mutex lock

怎甘沉沦 提交于 2019-12-11 18:13:05
问题 I never really worked with mutexes before, but i need to control access to protected resources. Looking through the new C++11 stuff, i cooked up this class: class CMutex { public: class Lockable { friend class CMutex; std::atomic_flag flag; public: Lockable() { flag.clear(); } }; private: Lockable * resource; CMutex(const CMutex &); public: CMutex(Lockable * l) { resource = l; acquire(l); } CMutex(Lockable & l) { resource = &l; acquire(l); } CMutex() : resource(nullptr) { } ~CMutex() { if

Using Both Conditional Variables & Mutex to Synchronize Threads in C

天涯浪子 提交于 2019-12-11 17:24:23
问题 Edited as per commenter's request. This program creates two threads. Each thread reads from one of two specific input files, each of which contains either one letter or one '0' per line of code. The threads are supposed to read the letters into a global char array, which is then printed. The problem is that, upon reaching a '0,' the active thread must transfer control to the other thread, which should not have a '0' on that line. (We are sure that, if File 1 has a '0' on a line, then File 2,

Boost threading/mutexs, why does this work?

痞子三分冷 提交于 2019-12-11 13:19:01
问题 Code: #include <iostream> #include "stdafx.h" #include <boost/thread.hpp> #include <boost/thread/mutex.hpp> using namespace std; boost::mutex mut; double results[10]; void doubler(int x) { //boost::mutex::scoped_lock lck(mut); results[x] = x*2; } int _tmain(int argc, _TCHAR* argv[]) { boost::thread_group thds; for (int x = 10; x>0; x--) { boost::thread *Thread = new boost::thread(&doubler, x); thds.add_thread(Thread); } thds.join_all(); for (int x = 0; x<10; x++) { cout << results[x] << endl;

Thread Locking in Ruby (use of soap4r and QT)

元气小坏坏 提交于 2019-12-11 10:52:49
问题 [EDIT NOTE: Noticed I had put the mutex creation in the constructor. Moved it and noticed no change.] [EDIT NOTE 2: I changed the call to app.exec in a trial run to while TRUE do app.processEvents() puts '." end I noticed that once the Soap4r service started running no process events ever got called again] [EDIT NOTE 3: Created an associated question here: Thread lockup in ruby with Soap4r I'm attempting to write a ruby program that receives SOAP commands to draw on a monitor (thus allowing