Re: C++ Threads, what's the status quo?
Zeljko Vrba wrote:
Wouldn't it be nice if you could instead of
--
pthread_mutex_lock(&lk);
x += 3; (x86 ASM: addl $3, x)
pthread_mutex_unlock(&lk);
--
write just 'x += 3;' and be *sure* that the compiler has generated
correct code (ie. used atomic instructions) without overheads of
function call,
mutexes, and potential sleeping and kernel entry. If the required
atomic code sequence cannot be generated for the target
architecture, the compiler would be *required* to report an error
instead of silently generating incorrect code as is the case now.
These things are not exactly equal. The mutex provides mutual
exclusion with other threads, while the atomic increment does not.
So, the following two fragments are not equivalent:
// ----------------------------------------------
int x;
pthread_mutex_t lk = PTHREAD_MUTEX_INITIALIZER;
void thread_1() {
pthread_mutex_lock(&lk);
x += 3;
pthread_mutex_unlock(&lk);
}
void thread_2() {
pthread_mutex_lock(&lk);
x = 12;
std::cout << x << std::endl;
pthread_mutex_unlock(&lk);
}
// ----------------------------------------------
// ----------------------------------------------
atomic int x;
pthread_mutex_t lk = PTHREAD_MUTEX_INITIALIZER;
void thread_1() {
x += 3;
}
void thread_2() {
x = 12;
std::cout << x << std::endl;
}
// ----------------------------------------------
You can only use atomic instructions without locks if all accesses to
the variable are of the form
pthread_mutex_lock(&lk);
x += n; // any operation for which there is
// an atomic instruction available
pthread_mutex_unlock(&lk);
I'm not an expert on compilers (nor on anything else for that
matter :)), but I would think that this can be detected by the
optimiser, and the compiler could generate atomic instructions under
the as-if rule.
That makes atomic instructions somewhat akin to inlining. You expect
the compiler to do it wherever it would make the programme faster
without affecting its correctness. Perhaps a compiler could have a
switch that makes it emit a notice whenever an atomic instruction is
used, so that you can debug performance issues. But you would only
think about them when optimising performance.
That seems to be the most natural solution to me.
Lourens
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]