mutex

What are the practical uses of semaphores?

巧了我就是萌 提交于 2019-12-14 00:18:41
问题 Nonbinary ones.. I have never encountered a problem that required me to use a semaphore instead of mutex. So is this mostly theoretical construct, or real sw like Office, Firefox have places where they use it? If so what are the common use patterns for semaphores? 回答1: I think you already know what a semaphore is and you are just wondering how it is used on "real software". "real sw like Office, Firefox have places where they use it?" Yes, "real software" use semaphores a lot, it is not just

QNX pthread_mutex_lock causing deadlock error ( 45 = EDEADLK )

爷,独闯天下 提交于 2019-12-13 20:43:26
问题 I am implementing an asynchronous log writing mechanism for my project's multithreaded application. Below is the partial code of the part where the error occurs. void CTraceFileWriterThread::run() { bool fShoudIRun = shouldThreadsRun(); // Some global function which decided if operations need to stop. Not really relevant here. Assume "true" value. while(fShoudIRun) { std::string nextMessage = fetchNext(); if( !nextMessage.empty() ) { process(nextMessage); } else { fShoudIRun =

Guaranteed yielding with pthread_cond_wait and pthread_cond_signal

两盒软妹~` 提交于 2019-12-13 15:23:47
问题 Assuming I have a C program with 3 POSIX threads, sharing a global variable, mutex, and condition variable, two of which are executing the following psuedocode: ...process data... pthread_mutex_lock( &mutex ); variable = data_ptr; pthread_cond_signal( &cond ); pthread_mutex_unlock( &mutex ); And the third running: while(1) { while( variable == NULL ) { pthread_mutex_wait( &cond, &mutex ); } printf( "Data is %d", *variable ); } Is it safe to assume that the third thread will see the data from

How do I box Arc and Mutexed variables?

二次信任 提交于 2019-12-13 10:22:25
问题 The following code gets some variables in closures and returns a struct containing that data. I can't return the struct with that data even when I box the struct and clone the variables; they are impossible to take out of this scope. I thought about using a callback closure but I don't really want to do that. Is there any way to take those out without having a callback? pub fn get(addr: &str) -> std::io::Result<Box<Response>> { use std::sync::{Arc, Mutex}; let mut crl = curl::easy::Easy::new(

How efficient is a try_lock on a mutex?

本秂侑毒 提交于 2019-12-13 07:53:24
问题 How efficient is a try_lock on a mutex? I.e. how much assembler instructions are there likely and how much time are they consuming in both possible cases (i.e. the mutex was already locked before or it was free and could be locked). In case you have problems to answer the question, here is a how to (in case that is really unclear): If that answer depends a lot on the OS implementation and hardware: Please answer it for common OS`s (e.g. Linux, Windows, MacOSX), recent versions of them (in

Producer/Consumer using Semaphore; getting deadlock

风流意气都作罢 提交于 2019-12-13 06:12:33
问题 According to http://en.wikipedia.org/wiki/Producer-consumer_problem I want to simulate P/C problem using semaphore. I am getting deadlock and I don't know what is problem. public static void main(String[] args) { CustomBlockingQueue blockingQueue = new CustomBlockingQueue(); new Thread(new Producer(blockingQueue)).start(); new Thread(new Consumer(blockingQueue)).start(); } } @SuppressWarnings("serial") class CustomBlockingQueue extends LinkedList<Object> { private static final int MAX_SIZE =

Map with concurrent access

我是研究僧i 提交于 2019-12-13 05:33:27
问题 When you use a map in a program with concurrent access, is there any need to use a mutex in functions to read values? 回答1: Multiple readers, no writers is okay: https://groups.google.com/d/msg/golang-nuts/HpLWnGTp-n8/hyUYmnWJqiQJ One writer, no readers is okay. (Maps wouldn't be much good otherwise.) Otherwise, if there is at least one writer and at least one more either writer or reader, then all readers and writers must use synchronization to access the map. A mutex works fine for this. 回答2

Multithreading File IO program behaves unpredictably when number of thread is increased

人走茶凉 提交于 2019-12-13 03:37:31
问题 Trying to create 1Mb(1048576Byte) file by writing in various chunk sizes and a different number of threads. When int NUM_THREADS = 2 or int NUM_THREADS = 1 then created file size is same as given i.e. 10MB . However when I increase thread count to 4, The created file size is around 400MB; Why this anomaly? #include <pthread.h> #include <string> #include <iostream> #define TenGBtoByte 1048576 #define fileToWrite "/tmp/schatterjee.txt" using namespace std; pthread_mutex_t mutexsum; struct

Dequeue from Queue with where expression

浪子不回头ぞ 提交于 2019-12-13 03:29:28
问题 I have a blocking queue class based on Marc Gravell's Blocking Queue Now I have situations where I want to dequeue only a certain object, I know this is not really the use case of a queue, but in some cases I think this is a good extension, for example waiting for a certain network answer. This would be something like a TryDequeueWhere(Func<T, bool> expression, out T value, int? waitTimeInMs = null) The problem is I don't know how to wait and block for a certain object. 回答1: After posting my

Calling `f()` once regardless of exceptions

余生颓废 提交于 2019-12-13 02:56:51
问题 My understanding may be incorrect but, reading the documentation for call_once , it appears that if multiple threads are calling it simultaneously with the same once_flag and the first thread throws an exception, one of the other threads will have its callable called (and so forth until one callable returns without throwing). My question is, if I have multiple thread all the with the same callable and I want the callable to be called truly once regardless of an exception and I want to know