mutex

Cross-platform and cross-process atomic int writes on file

删除回忆录丶 提交于 2019-12-03 22:22:59
问题 I'm writing an application that will have to be able to handle many concurrent accesses to it, either by threads as by processes. So no mutex'es or locks should be applied to this. To make the use of locks go down to a minimum, I'm designing for the file to be "append-only", so all data is first appended to disk, and then the address pointing to the info it has updated, is changed to refer to the new one. So I will need to implement a small lock system only to change this one int so it refers

How to make a method thread safe [closed]

我怕爱的太早我们不能终老 提交于 2019-12-03 22:01:26
Assume we have a method like this public static void method(string param) { ** critical section ** // There are a lot of methods calls // switch cases // if conditions // read and write in dictionary // new class initiations ** critical section ** } how we can make it thread safe while thousand of concurrent calls happen? Could delegates help? I read here that Modifying event is not thread-safe, but invoking a Delegate is thread-safe. Since a Delegate is immutable type so it is thread safe. Does that mean that delegates make my code thread safe? Here is a couple of concepts: in the above

C Confused on how to initialize and implement a pthread mutex and condition variable

帅比萌擦擦* 提交于 2019-12-03 21:43:34
I'm a little bit confused on how to initialize and implement a pthread mutex and condition variable. The goal of the program is to have producers place a set number of ints in a queue and consumers take the ints out of the queue. I must also be able to define the number of producer and consumer threads that are created. In the starter code, I am give these: // Locks & Condition Variables pthread_mutex_t lock; // Lock shared resources among theads pthread_cond_t full; // Condition indicating queue is full pthread_cond_t empty; // Condition indicating queue is empty as shared resources. In the /

Mutex are needed to protect the Condition Variables

笑着哭i 提交于 2019-12-03 20:44:59
As it is said that Mutex are needed to protect the Condition Variables. Is the reference here to the actual condition variable declared as pthread_cond_t OR A normal shared variable count whose values decide the signaling and wait. ? is the reference here to the actual condition variable declared as pthread_cond_t or a normal shared variable count whose values decide the signaling and wait? The reference is to both. The mutex makes it so that the shared variable ( count in your question) can be checked and if the value of that variable doesn't meet the desired condition the wait that is

“Could not find a part of the path” error while creating Mutex

醉酒当歌 提交于 2019-12-03 20:43:02
问题 I'm baffled by this, can someone tell me why, when I call: using (Mutex mtx = new Mutex(false, strId)) { } I get this exception: Could not find a part of the path. If strId is set to something like localhost\SQLEXPRESS-MyName-2 ? 回答1: From the docs: On a server that is running Terminal Services, a named system mutex can have two levels of visibility. If its name begins with the prefix "Global\", the mutex is visible in all terminal server sessions. If its name begins with the prefix "Local\",

Django: Simple rate limiting

那年仲夏 提交于 2019-12-03 19:28:24
Many of my views fetch external resources. I want to make sure that under heavy load I don't blow up the remote sites (and/or get banned). I only have 1 crawler so having a central lock will work fine. So the details: I want to allow at most 3 queries to a host per second, and have the rest block for a maximum of 15 seconds. How could I do this (easily)? Some thoughts : Use django cache Seems to only have 1 second resolution Use a file based semaphore Easy to do locks for concurrency. Not sure how to make sure only 3 fetches happen a second. Use some shared memory state I'd rather not install

How can I get rid of an abandoned mutex?

為{幸葍}努か 提交于 2019-12-03 18:18:54
问题 Summary: Is there a way to clear out a mutex if the process that created it is dead and gone? Details: I use a mutex to make sure that only one instance of my app runs. While testing out some new code (to do auto updating) I ran Environment.Exit(0). While running in debug mode, my mutex was cleaned up fine. But when I changed the build to be 'Release' then the mutex stays around after the exit call and is marked as abandoned: I have double checked to make sure that there is not a process

Can't provoke Priority Inversion in C++

ぐ巨炮叔叔 提交于 2019-12-03 17:34:43
I'm trying to provoke Priority Inversion on a small C++ program for demonstration purposes but I can't: The low priority thread that holds the mutex is not preempted and keeps running on the critical section. This is what I'm doing: // let's declare a global mutex pthread_mutex_t my_mutex; ... int main(int argc, char **argv) { ... pthread_t normal_thread; pthread_t prio_thread; pthread_mutexattr_t attr; pthread_mutexattr_init (&attr); pthread_mutexattr_setprotocol (&attr, PTHREAD_PRIO_NONE); // ! None ! pthread_mutex_init(&my_mutex, &attr); // create first normal thread (L): pthread_create(

Does QMutex need to be static so other threads calls of this class instance know to suspend their operations?

你离开我真会死。 提交于 2019-12-03 17:23:26
From multiple threads the following append function is called. I don't want data to re-write an append because the counter had not yet been incremented. Will this suspend all threads coming in except for the one currently using Append? Or will the other threads just continue running and not append the data? Does the mutex need to be "STATIC" or will each instance know to suspend operations? If I don't want hiccups, I assume I have to build a buffer to back log data? void classA::Append(int _msg) { static int c = 0; QMutex mutex; //need to be static so other threads know to suspend? //there are

mutexes with objects

别等时光非礼了梦想. 提交于 2019-12-03 17:05:48
I'm trying to understand how to use mutexes with objects in c++. I have the following (trivial) multi-threaded code I'm using as a speed test: struct Rope{ int n, steps, offset; //std::mutex mut; Rope() {} Rope(int n, int steps, int offset) : n(n), steps(steps), offset(offset) {} void compute(){ double a[n]; for (int i=0; i<n; i++) a[i] = i + offset; for (int step=0; step<steps; step++) for (int i=0; i<n; i++) a[i] = sin(a[i]); } }; void runTest(){ int numRuns = 30; int n = 10000; int steps = 10000; std::vector<Rope> ropes; std::vector<std::thread> threads; for (int i=0; i<numRuns; i++) ropes