mutex

How to avoid busy spinning in Java

a 夏天 提交于 2019-12-04 13:24:32
问题 I have a multi-threaded application where a thread sends a message to another thread. The waiting thread polls for the message and reacts (locks are handled). Like this: Waiting thread code: while(true) { if(helloArrived()) System.out.println("Got hello"); if(byeArrived()) System.out.println("Got bye"); if(stopArrived()) break; } I want to avoid this cpu hogging technique and use something else instead. Any ideas? Edit: The actual code is below: BlockingQueue<Mail> killMeMailbox = new

C# - Locking issues with Mutex

孤街浪徒 提交于 2019-12-04 13:14:47
I've got a web application that controls which web applications get served traffic from our load balancer. The web application runs on each individual server. It keeps track of the "in or out" state for each application in an object in the ASP.NET application state, and the object is serialized to a file on the disk whenever the state is changed. The state is deserialized from the file when the web application starts. While the site itself only gets a couple requests a second tops, and the file it rarely accessed, I've found that it was extremely easy for some reason to get collisions while

Implementing semaphore by using mutex operations and primitives

霸气de小男生 提交于 2019-12-04 13:13:18
问题 Some time ago had an interview and was asked to implement Semaphore by using mutex operations and primitives only (he allowed int to be considered as atomic). I came with solution below. He did not like busy/wait part -- while (count >= size) {} -- and asked to implement locking instead by using more primitive types and mutexes. I did not manage to come with improved solution. Any ideas how it could be done? struct Semaphore { int size; atomic<int> count; mutex updateMutex; Semaphore(int n) :

Ruby synchronisation: How to make threads work one after another in proper order?

坚强是说给别人听的谎言 提交于 2019-12-04 11:58:28
My problem is that I don't know how synchronise multiple threads using Ruby. The task is to create six threads and start them immediately. All of them should do some work (for example puts "Thread 1" Hi" ) one after another in the order I need it to work. I've tried to work with Mutex, Monitor and Condition Variable, but all of them worked in random order. Could anybody explain how to achieve my goal? After some time of struggling with Mutex and Condition Variable I've achieved my goal. This code is a little bit messy, and I intentionally did't use cycles for "clearer view". cv =

Starvation with upgrade_lock

耗尽温柔 提交于 2019-12-04 10:35:40
I am trying to use Boost's upgrade_lock (using this example , but I run into a starvation issue. I am actually using the code from this post , but I wanted an up-to-date discussion. I run 400 threads after the WorkerKiller . I run into the exact same problem as anoneironaut , the author of the mentionned post. I have seen the proposition from Howard Hinnant , but I don't really want to include more external code (moreover I cannot get his to compile as of now) and a comment posted 6 months later states that " Boost uses a fair implementation now " (Dec 3 '12). The Boost 1.55 documentation

How to use wait() and notify() in Java?

送分小仙女□ 提交于 2019-12-04 10:13:14
As I understand, I am suppose to call wait() on the mutex, when I want the current thread to stop working until another thread calls notify() on the same mutex object. That doesn't seem to be working. I'm trying to make a thread print 1-10. Then wait for another thread to print 11-20. And then the first thread would again print 21-30 Main.java public class Main { public static void main(String[] args) throws InterruptedException { Object mutex = 1; Thread child1 = new Thread(new Child1(mutex)); Thread child2 = new Thread(new Child2(mutex)); child1.start(); child2.start(); } } Child1.java

How Compare and Swap works

依然范特西╮ 提交于 2019-12-04 10:07:54
问题 I have read quite some posts that say compare and swap guarantees atomicity, However I am still not able to get how does it. Here is general pseudo code for compare and swap: int CAS(int *ptr,int oldvalue,int newvalue) { int temp = *ptr; if(*ptr == oldvalue) *ptr = newvalue return temp; } How does this guarantee atomicity? For example, if I am using this to implement a mutex, void lock(int *mutex) { while(!CAS(mutex, 0 , 1)); } how does this prevent 2 threads from acquiring the mutex at the

Cost of mutex,critical section etc on Windows

给你一囗甜甜゛ 提交于 2019-12-04 09:32:22
问题 I read somewhere that the overhead of a mutex is not that much, because the context switching only happens in case of contention. Also known Futexes in Linux. Does the same thing hold good in Windows? Is Critical Section a more apt map to mutexes in Linux. From what i gathered, Critical Sections provide better optimal performance compared to Mutex, is this true for every case? Is there a corner case where mutexes are faster than critical section in Windows. Assume only a single process

Is it possible to efficiently implement a seqlock in Java?

妖精的绣舞 提交于 2019-12-04 09:30:25
Another question made me wonder if the seqlock can be efficiently implemented with a volatile version counter in Java. Here's a prototypical implementation, for the case there will only ever be a single writer thread: class Seqlock { private volatile long version = 0; private final byte[] data = new byte[10]; void write(byte[] newData) { version++; // 1 System.arraycopy(newData, 0, data, 0, data.length); // 2 version++; // 3 } byte[] read() { long v1, v2; byte[] ret = new byte[data.length]; do { v1 = version; // 4 System.arraycopy(data, 0, ret, 0, data.length); // 5 v2 = version; // 6 } while

Why does std::mutex create a C2248 when used in a struct with WIndows SOCKET?

无人久伴 提交于 2019-12-04 08:29:13
I´m using a struct to support a list of Windows SOCKET´s: struct ConnectedSockets{ std::mutex lock; std::list<SOCKET> sockets; }; When i try to compile this (Visual Studio 2012) i get the following error: "Error C2248: std::mutex::operator = cannot access 'private' member declared in class 'std::mutex' " Does anybody know how to fix this? A std::mutex is not copyable, so you will need to implement the operator= for ConnectedScokets yourself. I presume you want to keep a mutex per instance of ConnectedSockets , so this should be enough: ConnectedSockets& operator =( ConnectedSockets const&