mutex

POSIX C Threads. Mutex example. Don't work as expected

一世执手 提交于 2019-12-10 03:01:50
问题 I have a big problem, I can't figure out why mutexes in C don't work as I expect. This is my code: #include <stdlib.h> #include <stdio.h> #include <pthread.h> pthread_t mythread; pthread_mutex_t mymutex; void *anotherFunc(void*) { pthread_mutex_lock(&mymutex); for(int i = 0; i < 100; i++) printf("anotherFunc\n"); pthread_mutex_unlock(&mymutex); pthread_exit(NULL); } void *func(void*) { pthread_mutex_lock(&mymutex); for(int i = 0; i < 100; i++) printf("func\n"); pthread_mutex_unlock(&mymutex);

Java thottling mechanism

扶醉桌前 提交于 2019-12-09 20:41:37
问题 Update : I'm on Java 1.6.34 with no chance of upgrading to Java 7. I have a scenario where I am only allowed to call a method 80 times per minute. It's actually a service API written by a 3rd party, and it "shuts down" (ignores calls) its API if you call it too many times: public class WidgetService { // Can only call this method 80x/min, otherwise it // it just doesn't do anything public void doSomething(Fizz fizz); } I'd like to write an ApiThrottler class that has a boolean canRun() method

Using mutexes/semaphores with processes

拥有回忆 提交于 2019-12-09 18:24:08
问题 Almost all the code and tutorials that I have read online so far involve using mutexes and semaphores for synchronisation amongst threads. Can they be used to synchronise amongst processes? I'd like to write code that looks like this: void compute_and_print() { // acquire mutex // critical section // release mutex } void main() { int pid = fork(); if ( pid == 0 ) { // do something compute_and_print(); } else { // do something compute_and_print(); } } Could someone point me towards similar

What does 'Mutex lock' exactly do?

给你一囗甜甜゛ 提交于 2019-12-09 17:07:15
问题 You can see an interesting table at this link. http://norvig.com/21-days.html#answers The table described, Mutex lock/unlock 25 nanosec fetch from main memory 100 nanosec Nanosec? I surprised because mutex lock is faster than fetch data from memory . If so, what mutex lock exactly do? And what does Mutex lock mean at the table? 回答1: The article you linked does not mentioned the architecture, but judging by mentions of L1 and L2 cache it's Intel. If this is so, then I think that by mutex they

How to synchronize method access(thread safe) by using Lock and delegates [closed]

我的未来我决定 提交于 2019-12-09 13:58:40
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 months ago . 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

Fastest Way for Java to write mutexes?

坚强是说给别人听的谎言 提交于 2019-12-09 13:05:20
问题 Mutexes are pretty common in many programming languages, like e.g. C/C++. I miss them in Java. However, there are multiple ways I could write my own class Mutex : Using a simple synchronized keyword on Mutex . Using a binary semaphore. Using atomic variables, like discussed here. ...? What is the fastest (best runtime) way? I think synchronized is most common, but what about performance? 回答1: Mutexes are pretty common in many programming languages, like e.g. C/C++. I miss them in Java. Not

std::mutex lock hangs when overriding the new operator

主宰稳场 提交于 2019-12-09 13:01:44
问题 We have an internal memory manager that we use with one of our products. The memory manager overrides the new and delete operators, and works fine in single-threaded appliations. However, I'm now tasked to make it work with multi-threaded applications too. To my understanding the following pseudo code should work, but it hangs on a spin, even with try_lock() . Any ideas? Update #1 Causes "Access Violation": #include <mutex> std::mutex g_mutex; /*! \brief Overrides the Standard C++ new

Why would we want to make a function recursive when it has a mutex lock?

半世苍凉 提交于 2019-12-09 06:49:24
https://stackoverflow.com/a/5524120/462608 If you want to call functions recursively, which lock the same mutex, then they either have to use one recursive mutex, or have to unlock and lock the same non-recursive mutex again and again (beware of concurrent threads!), or have to somehow annotate which mutexes they already locked (simulating recursive ownership/mutexes). Can in any case this be a "sensible" design decision to make function recursive which already has a mutex lock? In addition to paxdiablo's exmaple using an actual recursive funciotn, don't forget that using a mutex recursively

Do condition variables still need a mutex if you're changing the checked value atomically?

非 Y 不嫁゛ 提交于 2019-12-09 04:39:01
问题 Here is the typical way to use a condition variable: // The reader(s) lock(some_mutex); if(protected_by_mutex_var != desired_value) some_condition.wait(some_mutex); unlock(some_mutex); // The writer lock(some_mutex); protected_by_mutex_var = desired_value; unlock(some_mutex); some_condition.notify_all(); But if protected_by_mutex_var is set atomically by say, a compare-and-swap instruction, does the mutex serve any purpose (other than that pthreads and other APIs require you to pass in a

Robust CRITCAL_SECTION for shared memory?

痴心易碎 提交于 2019-12-08 19:55:35
问题 We have some data structures that we are sharing across processes on Windows. (Via a shared data segment in a DLL that's loaded by all these processes.) We need to synchronize some accesses and we measured that the performance hit of using a Win32 Mutex is too costly. CRITICAL_SECTION cannot be put into shared memory due to some of it's advanced features. This leaves us with the requirement of a simple locking/mutex solution based directly on the Interlocked* family of function on Win32.