mutual-exclusion

Mutex Stored Procedure

半城伤御伤魂 提交于 2019-12-01 06:50:20
问题 I want to create some distributed mutual exclusion using a database table. It would be nice to have the following interface on a stored procedure: Wait(uniqueidentifier) I was originally thinking of implementing this by having a table of unique identifiers. A call to the procedure would wait until the unique identifier does not exist in the table. However, I'm not sure how I would make the calling thread wake up when the specified unique identifier was removed from the table. Any ideas? If

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

怎甘沉沦 提交于 2019-11-28 18:43:34
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 file in /var/run/program_name.pid and check for existence, and compare pids if needed. There are probably more ways to do this. What do you think is the best approach? There are many ways to do it. PID files are the traditional way to do it. You could also hold a lock on a file, for example the program itself. This small piece of code will do the

Is my spin lock implementation correct and optimal?

安稳与你 提交于 2019-11-28 16:05:43
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() { while (__sync_lock_test_and_set(&exclusion, 1)) { // Do nothing. This GCC builtin instruction // ensures memory barrier. } } void unlock() { __sync_synchronize(); // Memory barrier. exclusion = 0; } So I'm wondering: Is this code correct? Does it correctly ensure mutual exclusion? Does it work on all x86 operating systems? Does it work on x86_64

Postgres constraint for unique datetime range

纵然是瞬间 提交于 2019-11-28 10:21:07
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). You can keep your separate timestamp columns and still use an exclusion constraint on an expression: CREATE TABLE tbl ( tbl_id serial PRIMARY KEY , starts_at timestamp , ends_at timestamp , EXCLUDE USING gist (tsrange(starts_at, ends_at) WITH &&) -- no overlapping ); Constructing a tsrange value without explicit bounds as tsrange(starts_at, ends_at) automatically assumes

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

送分小仙女□ 提交于 2019-11-27 11:47:24
问题 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 file in /var/run/program_name.pid and check for existence, and compare pids if needed. There are probably more ways to do this. What do you think is the best approach? 回答1: There are many ways to do it. PID files are the traditional way to do it.

std::unique_lock<std::mutex> or std::lock_guard<std::mutex>?

北城以北 提交于 2019-11-27 05:46:27
I have two use cases. A. I want to synchronise access by two threads to a queue. B. I want to synchronise access by two threads to a queue and use a condition variable because one of the threads will wait on content to be stored into the queue by the other thread. For use case A I see code example using std::lock_guard<> . For use case B I see code example using std::unique_lock<> . What is the difference between the two and which one should I use in which use case? The difference is that you can lock and unlock a std::unique_lock . std::lock_guard will be locked only once on construction and

Postgres constraint for unique datetime range

我们两清 提交于 2019-11-27 03:32:24
问题 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). 回答1: You can keep your separate timestamp columns and still use an exclusion constraint on an expression: CREATE TABLE tbl ( tbl_id serial PRIMARY KEY , starts_at timestamp , ends_at timestamp , EXCLUDE USING gist (tsrange(starts_at, ends_at) WITH &&) -- no overlapping )

Start Java program only if not already running

我怕爱的太早我们不能终老 提交于 2019-11-26 23:07:38
问题 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 application I don't care if any of them fail to start or not. They just need to fail silently. Here is what I have come up with so far: ProcessBuilder pb = new ProcessBuilder(userDefinedPath1); try { pb.start(); } catch (Exception e) { // Something

PHP mutual exclusion (mutex)

為{幸葍}努か 提交于 2019-11-26 22:14:42
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 delightful news? Edit: Due to some replies I've got here, I want to ask this; My script will run only by one thread, or several? Because if it's by one then I obviously don't need a mutex. Is there a concise answer? What exactly I'm trying to do Asked by ircmaxell.

Conditional Variable vs Semaphore

痞子三分冷 提交于 2019-11-26 21:11:38
When should one use a semaphore and when should one use a conditional variable (CondVar) ? Locks are used for mutual exclusion. When you want to ensure that a piece of code is atomic, put a lock around it. You could theoretically use a binary semaphore to do this, but that's a special case. Semaphores and condition variables build on top of the mutual exclusion provide by locks and are used for providing synchronized access to shared resources. They can be used for similar purposes. A condition variable is generally used to avoid busy waiting (looping repeatedly while checking a condition)