Questions tagged [test-and-set]
26 questions
17
votes
9 answers
What is Test-and-Set used for?
After reading the Test-and-Set Wikipedia entry, I am still left with the question "What would a Test-and-Set be used for?"
I realize that you can use it to implement Mutex (as described in wikipedia), but what other uses does it have?

Benoit
- 37,894
- 24
- 81
- 116
13
votes
5 answers
Atomic Instruction
What do you mean by Atomic instructions?
How does the following become Atomic?
TestAndSet
int TestAndSet(int *x){
register int temp = *x;
*x = 1;
return temp;
}
From a software perspective, if one does not want to use non-blocking…

Sashi
- 3,069
- 6
- 20
- 18
10
votes
3 answers
.NET memory model, volatile variables, and test-and-set: what is guaranteed?
I know that the .NET memory model (on the .NET Framework; not compact/micro/silverlight/mono/xna/what-have-you) guaranteed that for certain types (most notably primitive integers and references) operations were guaranteed to be atomic.
Further, I…

Robert Fraser
- 10,649
- 8
- 69
- 93
7
votes
3 answers
.Net CompareExchange reordering
Can the compiler or processor reorder the following instructions so that another Thread sees a == 0 and b == 1?
Assuming int a = 0, b = 0; somewhere.
System.Threading.Interlocked.CompareExchange(ref a, 1,…

ominug
- 1,422
- 2
- 12
- 28
4
votes
0 answers
Mutex implementation without hardware support of Test-and-Set and CAS-operations
Let's we have the following simple mutex definition:
class Mutex {
private:
bool lock;
public:
void acquire();
void release();
Mutex() {
lock = 0;
}
};
And the following acquire() func realization (with using of…

TwITe
- 400
- 2
- 14
3
votes
2 answers
Implementing a mutex with test-and-set atomic operation: will it work for more than 2 threads?
I'm reading the Wikipedia article on the test-and-set atomic operation. It says that one way to implement mutual exclusion is by using a test-and-set based lock.
However, according to the same article, the test-and-set operation has a finite…

Ignorant
- 2,411
- 4
- 31
- 48
3
votes
6 answers
How to process each item in an ordered list with duplicates only once in Python?
I have an ordered list of things to process that includes some duplicates and I only want to process the first occurrence. Presently, I'm doing it like this in Python v2.7:
seen = set()
for (value, fmt) in formats:
if fmt not in seen:
…

WilliamKF
- 41,123
- 68
- 193
- 295
3
votes
1 answer
Atomic operations over a list
Suppose I have a list, and I want to use a test_and_set operation a parameter of which is the calculation of some pointer address l->a.next->next. This, I think, will not be atomic, and the test_and_set will be useless. Is there a way to calculate…

Dervin Thunk
- 19,515
- 28
- 127
- 217
3
votes
3 answers
Lock free atomic state class - is it correct?
I am just looking for feedback (obvious flaws/ways to improve it) on my attempt to implement atomic read/writes on a structure.
There will be one writing thread and multiple reading threads. The aim is to prevent a reader from getting an…

willcode.co
- 674
- 1
- 7
- 17
2
votes
1 answer
Need help understanding implementation of mutex with test_and_set
The book Operating System Principles by Silberschatz, Galvin, and Gagne has the following implementation for atomic operations of test_and_set
boolean test_and_set(boolean *target) {
boolean rv = *target;
*target = true;
return…

Satyam Yadav
- 21
- 4
2
votes
3 answers
PHP flock() for read-modify-write does not work
I have a log file maintained by a PHP script. The PHP script is subject to parallel processing. I cannot get the flock() mechanism to work on the log file: in my case, flock() does not prevent the log file shared by PHP scripts running in parallel…

PaulH
- 2,918
- 2
- 15
- 31
2
votes
0 answers
How to analyze a mutual exclusion algorithm with two atomic test-and-set calls
Someone has posted on this site a simple mutual exclusion algorithm for two threads.
bool locks[2];
thread_function() {
bool id = get_id();
while(1) {
if (!tset(&locks[id])) {
if (!tset(&locks[!id])) {
x++; // critical…

Dima Chubarov
- 16,199
- 6
- 40
- 76
1
vote
1 answer
Semaphores with test and set (code implementation possible mistake)
I have been learning about semaphores and I was looking at a website implementation of semaphores (http://faculty.salina.k-state.edu/tim/ossg/IPC_sync/ts.html), however, I don't understand the implementation, to save anyone going to the website the…

james king
- 90
- 2
- 9
1
vote
1 answer
does the atomic instruction involve the kernel
I'm reading this link to learn about futex of Linux. Here is something that I don't understand.
In order to acquire the lock, an atomic test-and-set instruction (such
as cmpxchg()) can be used to test for 0 and set to 1. In this case,
the…

Yves
- 11,597
- 17
- 83
- 180
1
vote
1 answer
Is gcc's atomic test and set builtin the same as an atomic fetch and store operation?
I came across an atomic "fetch and store" instruction in the description of an MCS lock.
From what I gather, this atomically writes a value to a memory location and returns the original value of that memory location, is that correct?
And is gcc's…

Jimmeh
- 1,011
- 9
- 14