mutex

two threads going to do func()

2 threads going to use the same func(). The 2 threads should be mutually exclusive. How do I get it to work properly? (output should be "abcdeabcde") char arr[] = "ABCDE"; int len = 5; void func(){ for(int i = 0; i <len;i++) printf("%c,arr[i]); } ...

ASP.NET:Problem with race conditon in business logic

I have the following problem: Our system has products that when released only are allowed to be purchased X times. Upon purchase a central purchasing algorithm checks how many Orders exist and if below X proceeds with the purchase. In pseudoish C# code: public class OrderMethods { public static Purchase(Product product, Client c...

Should mutex.WaitOne() inside or before the try/finally block.

Hi Guys, I was wondering which of the following was the suggested pattern when using Mutex (or Semaphores or ReadWriteLockSlims etc.). Should the initial lock happen inside or outside of the try statement? Is it unimportant? _mutex.WaitOne() try { // critical code } finally { _mutex.ReleaseMutex(); } or try { _mutex.WaitOne...

What is the correct pattern for using a global mutex in WPF?

So I've found this information http://stackoverflow.com/questions/229565/what-is-a-good-pattern-for-using-a-global-mutex-in-c/229567 But it's based on the main entry point of a standard app. How do you modify this to work for the WPF event model of starting the app? ...

Modelling boost::Lockable with semaphore rather than mutex (previously titled: Unlocking a mutex from a different thread)

I'm using the C++ boost::thread library, which in my case means I'm using pthreads. Officially, a mutex must be unlocked from the same thread which locks it, and I want the effect of being able to lock in one thread and then unlock in another. There are many ways to accomplish this. One possibility would be to write a new mutex class ...

Why do pthreads’ condition variable functions require a mutex?

I’m reading up on pthread.h; the condition variable related functions (like pthread_cond_wait(3)) require a mutex as an argument. Why? As far as I can tell, I’m going to be creating a mutex just to use as that argument? What is that mutex supposed to do? ...

[Delphi] open text files in one application

hi all I want to write an text editor and to assign the txt files to it. My problem is that I want to have only one instance running and when a new file is opened to send the filename to the first app that is already running... (I want to do this using mutex). Here is a small test DPR looks like this uses Windows, Messages, SysUtils,...

using Mutex causing application to hang on Win XP X64

hi. I used the following code to verify the single instance of application. On Win XP X86 it is working fine, but on X64 after 3 to 4 minutes System generates StackOverflowException and causes the application to hang. after removing this check application is working fine.. Please tell me what should be the reason. code is static voi...

C++ Mutexes and STL Lists Across Subclasses

I am currently writing a multi-threaded C++ server using Poco and am now at the point where I need to be keeping information on which users are connected, how many connections each of them have, and given it is a proxy server, where each of those connections are proxying through to. For this purpose I have created a ServerStats class wh...

cross-user C# mutex

My app is forced to use a 3rd party module which will blue-screen Windows if two instances are started at the same time on the same machine. To work around the issue, my C# app has a mutex: static Mutex mutex = new Mutex(true, "{MyApp_b9d19f99-b83e-4755-9b11-d204dbd6d096}"); And I check if it's present - and if so I show an err...

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

Hello! 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 ad...

Maintaining single instance application

Hello. I have been working on this application of mine and got this problem. Running program through command line with different arguments opens different .exe process. My question is how can i prevent from opening same file few times, and is it possible to send new command line arguments to already open instance of application. Thanks...

ARM cortex: mutex using bit banding

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...

Mutex takes a long while to unlock

I have two threads. First one is something like this: while(1) { pthread_mutex_lock(&mutex); //DO WORK pthread_mutex_unlock(&mutex); pthread_yield(); } The second one locks the mutex on user event, change some settings and unlocks. Thread one does ~ 200 iterations a second. However on occasion it takes the second thread up...

How is spin lock implemented under the hood?

This is a lock that can be held by only one thread of execution at a time. An attempt to acquire the lock by another thread of execution makes the latter loop until the lock is released. How does it handle the case when two threads try to acquire the lock exactly the same time? I think this question also applies to variou...

How can I improve my real-time behavior in multi-threaded app using pthreads and condition variables?

I have a multi-threaded application that is using pthreads. I have a mutex() lock and condition variables(). There are two threads, one thread is producing data for the second thread, a worker, which is trying to process the produced data in a real time fashion such that one chuck is processed as close to the elapsing of a fixed time p...

How to multi-thread this?

I wish to have two threads. The first thread1 occasionally calls the following pseudo function: void waitForThread2() { if (thread2 is not idle) { return; } notifyThread2IamReady(); // i.e. via 1st condition variable Wait for thread2 to finish exclusive access. // i.e. via 2nd condition variable. } The second thread2 is f...

Implement a multithreading environment

I want to implement a multithreading environment using Qt4. The idea is as follows in c++-alike pseudo-code: class Thread : public QThread { QList<SubThread*> threads_; public: void run() { foreach(SubThread* thread : threads) { thread.start(); } foreach(SubThread* thread : threads) { ...

PHP mutual exclusion (mutex)

Read some texts about locking in PHP. They all, mainly, direct to http://php.net/manual/en/function.flock.php . This page talks about opening a file on the hard-disk!! Is it really so? I mean, this makes locking really expensive - it means each time I want to lock I'll have to access the hard-disk )= Can anymore comfort me with a deli...

PHP rewrite an included file - is this a valid script?

Hi all! I've made this question: http://stackoverflow.com/questions/2921469/php-mutual-exclusion-mutex As said there, I want several sources to send their stats once in a while, and these stats will be showed at the website's main page. My problem is that I want this to be done in an atomic manner, so no update of the stats will overla...