Questions tagged [mutual-exclusion]

Mutual exclusion means that processes must always execute critical sections in time intervals that do not overlap. The mutual exclusion requirement is one of the most basic in an operating system; without it the OS cannot share resources safely.

261 questions
505
votes
7 answers

std::unique_lock or std::lock_guard?

I have two use cases. A. I want to synchronise access to a queue for two threads. B. I want to synchronise access to a queue for two threads and use a condition variable because one of the threads will wait on content to be stored into the queue by…
chmike
  • 20,922
  • 21
  • 83
  • 106
157
votes
8 answers

Conditional Variable vs Semaphore

When to use a semaphore and when to use a conditional variable?
156
votes
24 answers

Make sure only a single instance of a program is running

Is there a Pythonic way to have only one instance of a program running? The only reasonable solution I've come up with is trying to run it as a server on some port, then second program trying to bind to same port - fails. But it's not really a…
Slava V
  • 16,686
  • 14
  • 60
  • 63
40
votes
11 answers

Is my spin lock implementation correct and optimal?

I'm using a spin lock to protect a very small critical section. Contention happens very rarely so a spin lock is more appropriate than a regular mutex. My current code is as follows, and assumes x86 and GCC: volatile int exclusion = 0; void lock()…
Hongli
  • 18,682
  • 15
  • 79
  • 107
32
votes
4 answers

Mutex alternatives in swift

I have a shared-memory between multiple threads. I want to prevent these threads access this piece of memory at a same time. (like producer-consumer problem) Problem: A thread add elements to a queue and another thread reads these elements and…
rick
  • 1,009
  • 2
  • 10
  • 28
28
votes
5 answers

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…
Poni
  • 11,061
  • 25
  • 80
  • 121
25
votes
3 answers

What's the best way to make sure only one instance of a Perl program is running?

There are several ways to do this, but I'm not sure which one of them is the best. Here's what I can think of: Look for the process using pgrep. Have the script lock itself using flock, and then check if it is locked each time it runs. Create a pid…
Tom Feiner
  • 20,656
  • 20
  • 48
  • 51
22
votes
2 answers

Difference between mutual exclusion and synchronization?

What is the difference between above two? This question came to my mind because I found that Monitors and locks provide mutual exclusion Semaphores and conditional variables provide synchronization Is this true? Also while searching I…
Tanya
  • 354
  • 1
  • 3
  • 14
19
votes
8 answers

Mutual-exclusion using TestAndSet() instruction

The book Operating System Principles by Silberschatz, Galvin and Gagne contains the following definition for the TestAndSet() instruction in the chapter on synchronization: boolean TestAndSet(boolean *target) { boolean rv = *target; *target…
Gaurav
18
votes
1 answer

Postgres constraint for unique datetime range

My table has two columns: startsAt endsAt Both hold date and time. I want to make following constraint: IF both columns are NOT NULL then range between startsAt and endsAt must not overlap with other ranges (from other rows).
user606521
  • 14,486
  • 30
  • 113
  • 204
18
votes
6 answers

Static Variables and Threads (C)

I know that declaring a static variable within a function in C means that this variable retains its state between function invocations. In the context of threads, will this result in the variable retaining its state over multiple threads, or having…
dahui
  • 2,128
  • 2
  • 21
  • 40
17
votes
8 answers

Start Java program only if not already running

I need to start 1-3 external programs in my Java application that have paths defined by the user. I have few requirements: I don't want the program to execute if it is already running I don't want any of the programs to steal focus from my Java…
Morinar
  • 3,460
  • 8
  • 40
  • 58
13
votes
2 answers

How to create mutually exclusive table columns

I have a table Transactions where I am saving two records for one transaction, one for debiting and other crediting. So I have two columns in table creditAmount(Money) and debitAmount(Money). I want a table level constraint that either of the column…
MaxRecursion
  • 4,773
  • 12
  • 42
  • 76
12
votes
3 answers

Mutually exclusive regular expressions

If I have a list of regular expressions, is there an easy way to determine that no two of them will both return a match for the same string? That is, the list is valid if and only if for all strings a maximum of one item in the list will match the…
captncraig
  • 22,118
  • 17
  • 108
  • 151
11
votes
9 answers

Difference Between Monitor & Lock?

What's the difference between a monitor and a lock? If a lock is simply an implementation of mutual exclusion, then is a monitor simply a way of making use of the waiting time inbetween method executions? A good explanation would be really helpful…
Goober
  • 13,146
  • 50
  • 126
  • 195
1
2 3
17 18