Re: Thread question



Paul Pluzhnikov wrote:
Mihai Osian <zzz@xxxxxxx> writes:

I was just showing what types of variables need to be
synchronized. I assumed that anybody understands what read-only
access implies. You assumed that both me and the OP are incompetent.

The OP doesn't understand that local variables are not shared between
threads, and you assume he understands what read-only access implies?

Bad assumption (IMHO).

Mea culpa.

You also told him:

printf("%s\n", s); // use mutex

So if he was to follow your advice, he'll do this:

pthread_mutex_lock(&mtx);
printf("%s\n", s);
pthread_mutex_unlock(&mtx);

Is *that* what you advised him to do?

"yes" in the general case, "no" in my little example.
We both know. Let's drop the subject.


That's BS. Sorry, I mean "bogus". For an aligned-volatile-32-bit
value the change is _probably_ atomic, depending on the platform
and the compiler.

Name a Linux compiler that doesn't align pointers and integers [1].
Name a Linux compiler where pointer or integer is not atomic.
Name *any* OS/compiler coimbination that the OP has any chance of
encountering, where pointer or integer is not atomic.

Well, gcc/m68k for example. But I admit - it is highly unlikely the original OP would ever use such a thing.
So yes, I was wrong about the int alignment requirement.



And volatile has absolutely nothing to do with this (more BS^H^H
bogus advice).


Based on the content of my "/usr/src/linux/include/asm/atomic.h",
Linus Torvalds seems to disagree with you.

Now, let's say he just forgot to remove the "volatile" from his atomic_t structure. Look at this page: http://www.kcomputing.com/volatile.html. They provide a 50 line test program. Compile it yourself according to their instructions. You can use a single-processor machine, the result will still surprise you.
Then come back an tell us the result.


Cheers,

[1] Because this was asked in comp.os.*linux*.d.apps


Going back to your initial answer to the OP: you told him to access the variables freely, without synchronization. That's ok, but you should have explained him that not any variable may be accessed freely.
Talking about assumption - would you expect the OP to know that the size of the variable is very important in this case ?

Apart from our little disagreement: do use volatile in your multithreaded programs. It is important. And think twice before calling someone's answer "bogus". There might be a chance that you are wrong.

My apologies for the BS, by the way.
Mihai


.