mutex

Thread-safe way to build mutex protection into a C++ class?

∥☆過路亽.° 提交于 2019-12-06 07:49:32
问题 I'm trying to implement a producer/consumer model multithreaded program in C++ for a project I'm working on. The basic idea is that the main thread creates a second thread to watch a serial port for new data, process the data and put the result in a buffer that is periodically polled by the main thread. I've never written multi-threaded programs before. I've been reading lots of tutorials, but they're all in C. I think I've got a handle on the basic concepts, but I'm trying to c++ify it. For

C# - Locking issues with Mutex

扶醉桌前 提交于 2019-12-06 06:27:17
问题 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

Use mutex or not in a concurrent reading

醉酒当歌 提交于 2019-12-06 06:05:06
I am programming in C++ in Linux and I am using pthreads library. I am using mutex to protect some shared variables but I am not sure if in this specific case it is necessary the use of mutex. I have 3 threads. The shared variable is a string (global variable). Thread1 changes it's value and afterwards, thread2 and thread3 read it's value and store in another string. In this case, the string's value is only modified by one thread. Is still necessary the use of mutex to protect a shared variable in a concurrent read by two threads? "Thread1 changes it's value and afterwards ..." -- if

Is it possible to efficiently implement a seqlock in Java?

非 Y 不嫁゛ 提交于 2019-12-06 04:37:29
问题 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];

ARM cortex: mutex using bit banding

与世无争的帅哥 提交于 2019-12-06 04:01:17
问题 Given that, on the ARM Cortex M3, I can: atomically read a single bit atomically set a single bit atomically clear a single bit How can I combine these for a mutex style set of operations: try lock take lock release lock It seems that try_lock or take_lock would require two operations that would not be atomic. Do I need more control to accomplish this? Disable global interrupts would do it but it seems there should be a more surgical approach. 回答1: Your rwl_TryLock() doesn't necessarily

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

余生颓废 提交于 2019-12-06 03:13:19
问题 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? 回答1: 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

pthread_cleanup_push causes Syntax error

随声附和 提交于 2019-12-06 01:34:49
I try to add a section to my code which is able to unlock the mutex in a case of cancellation. This may happen and would cause a deadlock. Therefore I tried to add pthread_cleanup_push(cleanup_unlock_mutex, &mutex_ftdi); but this line cause a syntax error from the line where I add it till the end of the code file. If I comment the code line the program will compile without any error. What I'm doing wrong? void cleanup_unlock_mutex(void *p){ pthread_mutex_unlock(p); } ....... }else{ for(unsigned count=0; count <= number_of_requests; count++){ pthread_cleanup_push(cleanup_unlock_mutex, &mutex

C++ atomic with non-trivial type?

こ雲淡風輕ζ 提交于 2019-12-05 23:39:57
问题 Reading the docs on boost::atomic and on std::atomic leaves me confused as to whether the atomic interface is supposed to support non-trivial types? That is, given a (value-)type that can only be written/read by enclosing the read/write in a full mutex, because it has a non-trivial copy-ctor/assignment operator, is this supposed to be supported by std::atomic (as boost clearly states that it is UB). Am I supposed to provide the specialization the docs talk about myself for non-trivial types?

How does this recursive synchronized call not deadlock?

左心房为你撑大大i 提交于 2019-12-05 23:30:50
I have a set of methods that all synchronize to the class object (can't use self, because multiple instances of this object could be used in multiple threads). Some of those methods call other methods in the class that also synchronize on the class object. Somehow this works and does not cause the deadlock I would expect it to. I would assume that testA would be blocked from running because testB already has a lock on the class object, but this apparently isn't the case. Is it something special that @synchronized is doing or is this a feature of the underlying mutex locks? Example code that

One reader. One writer. Some general questions about mutexes and atomic-builtins

时光毁灭记忆、已成空白 提交于 2019-12-05 23:22:18
问题 I have a parent and a worker thread that share a bool flag and a std::vector. The parent only reads (i.e., reads the bool or calls my_vector.empty()); the worker only writes. My questions: Do I need to mutex protect the bool flag? Can I say that all bool read/writes are inherently atomic operations? If you say Yes or No, where did you get your information from? I recently heard about GCC Atomic-builtin. Can I use these to make my flag read/writes atomic without having to use mutexes? What is