mutex

All the Swing frames get “frozen” when wait() is called in Java

≯℡__Kan透↙ 提交于 2019-12-24 14:51:25
问题 I want to wait() the put() method called from the second thread which has been connected to the Server (Monitor). But when i do this, the whole GUI frames (Swing) including their elements get frozen aftr the second put() call. How to fix this? I want the second thread keep waiting till the first thread performs a get() which frees a slot. Thanks in advance. Here's my skeleton code: Server: Buffer<String> buf = new Buffer<String>(1); while(true){ //for each socket connected new ServerHandler(.

One thread showing interest in another thread (consumer / producer)

冷暖自知 提交于 2019-12-24 12:38:15
问题 I would like to have to possibility to make thread (consumer) express interest in when another thread (producer) makes something. But not all the time. Basically I want to make a one-shot consumer. Ideally the producer through would go merrily about its business until one (or many) consumers signal that they want something, in which case the producer would push some data into a variable and signal that it has done so. The consumer will wait until the variable has become filled. It must also

mutex and threads independence

半世苍凉 提交于 2019-12-24 11:44:39
问题 I run the following program on a 32 cores computer: #include<iostream> #include<algorithm> #include<boost/thread.hpp> using namespace std; boost::thread_group g; boost::mutex _mtx; class A{ public: void foo() { for(int ix = 0; ix < 10000000; ++ix) vec.push_back(ix); sort(vec.rbegin(), vec.rend()); } private: vector<int> vec; }; void thread_fun() { A a; _mtx.lock(); //line 24 a.foo(); _mtx.unlock(); //line 26 } int main() { g.add_thread(new boost::thread(thread_fun)); g.add_thread(new boost:

Check optional mutex before scoped locking

老子叫甜甜 提交于 2019-12-24 10:56:42
问题 I have a constructor that optionally allows the user to pass a ponter to a Boost mutex. If no mutex is supplied, the member pointer pMyMutex is set to NULL . This gives the user the option of applying some thread safety if they wish. However, I cannot use a scoped_lock with this kind of check for obvious reasons :) if (pMyMutex != NULL) const boost::mutex::scoped_lock l(*pMyMutex); //The lock is already out of scope processStuff(x, y, z); Can anyone suggest a neat and simple solution to such

Releasing a named mutex created in WPF Application.OnStartUp(): Which thread owns it?

纵饮孤独 提交于 2019-12-24 05:22:09
问题 I create a mutex within the OnStartup Method of a WPF app. The mutex is not used anywhere else in the program, its only purpose is to prevent certain programs from running concurrently. How can I release this mutex when the application closes? According to the documentation, mutex.ReleaseMutex() must be called from the same thread that created the mutex. However this presents a problem, since I do not control the thread that calls OnStartup() . Suppose my OnStartup method looks like this:

What is the Mutex acquire and release order?

一曲冷凌霜 提交于 2019-12-24 04:16:12
问题 I know the functionality of Mutex. But now I am confused about its timing. I specially mean in the Linux kernel code. For example, we have 3 threads (let's say they are on the same processor and are all normal tasks with the same priorities). Thread 1 ,2 and 3 try to acquire the Mutex and only Thread 1 gets it. Thread 2 and 3 are blocked and go to sleep. Then Thread 1 has done his job and unlock the Mutex. So here is my question: At this very moment, what will happen? Will Thread 1 continue

Producer Consumer using semaphores and mutexes in Python

社会主义新天地 提交于 2019-12-24 03:52:40
问题 I'm trying to understand how to implement a Queue with a bounded buffer size that can be used by multiple producers and consumers using Python Semaphores. Here's my implementation: class Q: def __init__(self, size): self.buff = [None]*size self.end = 0 self.start = 0 self.size = size self.end_lock = Lock() # protect end from race across multiple producers self.start_lock = Lock() # protect start from race across multiple consumers self.open = Semaphore(size) # block till there's space to

No deadlock unless linked to pthreads?

…衆ロ難τιáo~ 提交于 2019-12-24 01:33:46
问题 Why is it that creating a std::mutex deadlock will not actually cause a deadlock unless the program is linked to pthreads? The following will deadlock when linked with pthreads library and will not deadlock if pthreads is not linked in. Tested on gcc and clang. // clang++ main.cpp -std=c++14 -lpthread #include <mutex> int main() { std::mutex mtx; mtx.lock(); mtx.lock(); return 0; } I understand that without a thread library you don't actually need mutex functionality, but is the compiler

Can I use mutex or key word(static) instead of volatile in C++?

一个人想着一个人 提交于 2019-12-24 01:33:14
问题 I know how volatile works in C++. However, I'm still feeling confused about why we need volatile. Only reason I think is we need listen whether the variable changed, as the code shows below: volatile int some_int = 100; while(some_int==100) { doSomething() } The programmer needs know if the word some_int changed, so he uses "volatile". Could I use the key word "static" instead? Mostly, I think we should we mutex(lock) to synchronize if synchronization is necessary in multi-thread. Excuse my

What is bus-locking in the context of atomic variables?

落花浮王杯 提交于 2019-12-24 01:09:06
问题 I use C++ since a long time, and now I'm starting to learn assembly and learn how processors work (not just for fun, but I have to as a part of a test program). While learning assembly, I started hearing some of the terms that I hear here and there when discussing multithreading, given that I do lots of multithreading in scientific computing. I'm struggling to get the full picture, and I'd appreciate helping me to widen my picture. I learned that a bus, in its simplest form, is something like