mutual-exclusion

Conditional Variable vs Semaphore

岁酱吖の 提交于 2019-12-17 05:16:13
问题 When should one use a semaphore and when should one use a conditional variable (CondVar) ? 回答1: 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

Using SQL dB column as a lock for concurrent operations in Entity Framework

跟風遠走 提交于 2019-12-13 02:57:44
问题 We have a long running user operation that is handled by a pool of worker processes. Data input and output is from Azure SQL. The master Azure SQL table structure columns are approximated to [UserId, col1, col2, ... , col N, beingProcessed, lastTimeProcessed ] beingProcessed is boolean and lastTimeProcessed is DateTime. The logic in every worker role is as shown below and with multiple workers processing (each with their own Entity Framework layer), in essence beingProcessed is being used a

PHP Mutual Exclusion on a File / MySQL reading and executing statements from a file using perl

隐身守侯 提交于 2019-12-13 02:42:24
问题 So I was wondering how / if PHP has some sort of mutual exclusion on file reading and writing. Here's how I plan on using it: The site I'm working with utilizes a payment service that requires leaving the server, making it difficult to deal with form submissions, such that the form does not get submitted into the database until after returning from the payment service. Information CAN be passed through the payment service and regurgitated on the other end. However, there is minimum

How spinlock prevents the process to be interrupted?

柔情痞子 提交于 2019-12-12 01:21:57
问题 I read an answer on this site says the spin-lock reduce the overhead with context switches, and after that I read an textbook statement related to this: Spin-lock makes a busy waiting program not be interrupted. My question is on the title. Since the book uses while-loop to indicate the implementation of the spin part of a spin-lock, the following is my reasoning trying to explain myself with this consideration. This sounds like if there is a program with a busy waiting while loop then all

Programming a mutex in Java

谁说我不能喝 提交于 2019-12-10 21:39:59
问题 I'm new to Computer Science and I'm reading a book which introduces threads and mutexes. I've tried programming a mutex in Java which seems to work most of the time but every so often it won't. In my code, the critical section adds on the numbers 1 to 10 to a static variable j which results in 55 (if j starts at 0). If I run three threads at the same time through the critical section, I get random final values of j which makes sense. But with the mutex below, most of the time I get a final j

Exit critical region

僤鯓⒐⒋嵵緔 提交于 2019-12-10 20:28:53
问题 Consider several threads executing concurrently the following code: long gf = 0;// global variable or class member //... if (InterlockedCompareExchange(&gf, 1, 0)==0) // lock cmpxchg { // some exclusive code - must not execute in concurrent gf = 0; // this is ok ? or need //InterlockedExchange(&gf, 0); // [lock] xchg } Treat the code above as C-like pseudo-code, which will be translated more-or-less directly into assembly without the usual concessions to compiler optimizations such as re

Making pthread_rwlock_wrlock recursive

断了今生、忘了曾经 提交于 2019-12-10 05:26:56
问题 I have a problem regarding the behaviour of the pthread function pthread_rwlock_wrlock. The specification linked above states that when one thread has locked the lock for writing and the same thread locks it again, it results in undefined behaviour (I could actually observe this in that on x86 Linux calling this function is a noop and on PowerPC Linux it stalls the thread). The behaviour I need would be a read write lock that has the following characteristics: read-locking by a thread

Static Variables and Threads (C)

て烟熏妆下的殇ゞ 提交于 2019-12-09 08:38:39
问题 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 a separate state between each thread ? Here is a past paper exam question I am struggling to answer: The following C function is intended to be used to allocate unique identifiers (UIDs) to its callers: get_uid() { static int i = 0; return i++; }

Can Test and Set be implemented in software without hardware support?

人盡茶涼 提交于 2019-12-08 19:53:19
问题 Here's the Test and Set written in software: boolean TestAndSet(boolean *target) { boolean rv = *target; *target = TRUE; return rv; } and do { while(TestAndSetLock(&lock)) ; // do nothing // critical section lock = FALSE; // remainder section } while(TRUE); Can we use the mechanism in CPUs that do not support test-and-set at the hardware level? If so, how is atomicity assured? 回答1: You can use Lamport's 'bakery' mutual exclusion algorithm on machines w/o TAS/CAS to gate access to the 'atomic'

What is equivalent of the C# lock statement in PHP?

穿精又带淫゛_ 提交于 2019-12-07 14:50:14
问题 For concurrency and ensuring the integrity of the data, how would you obtain a mutual-exclusion lock for a given object? Would you need to use locking within the database, or a file, or does PHP support something like this? 回答1: PHP doesn't support multithreading so there's no locking mechanism for objects. If you want to lock a file you could use flock for that. There's no need to lock database as database engines usually can handle multiple connections. 回答2: Bare in mind PHP is not