L
L
likenoother2010-12-01 19:32:14
Data synchronization
likenoother, 2010-12-01 19:32:14

atomicity in C++

Good day!

There was a problem: it is necessary to synchronize flows through testAndSet: But here with atomic a problem, and without it in any way. Can you tell me a library for atomically changing a variable in C++?
int atomic testAndSet ( char *lock ) {
char var;
var = *lock;
*lock = 1;
return var;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
Tujh, 2010-12-01
@likenoother

Without cross-platform requirements, you can use WInAPI and its Interlocked* functions.

M
mt_, 2010-12-01
@mt_

I won't answer your specific question. But I myself write in the U ++ framework, where all these things are present in a cross-platform form:
Atomic t;
AtomicWrite(t, 1);

A
apangin, 2010-12-01
@apangin

GCC has built-in functions like _sync_val_compare_and_swap().
Under Solaris, you can #include <atomic.h> where there are functions like atomic_cas().
Under Windows - API from the InterlockedCompareExchange () category.
I don't know of a universal solution. Several times I met inline assembler for this case under #ifdefs for different platforms.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question