Re: Thread question
- From: Mihai Osian <zzz@xxxxxxx>
- Date: Fri, 26 Jan 2007 23:05:52 +0100
Paul Pluzhnikov wrote:
Mihai Osian <zzz@xxxxxxx> writes:
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.
No, he doesn't. The page you are referring to deals with signals,
and is not relevant to current discussion.
Well, maybe. I am not a kernel expert.
Going back to your initial answer to the OP: you told him to access
the variables freely, without synchronization.
I didn't. I merely said your advice was incorrect.
"Even if other thread(s) did change them, mutex is *still* not
necessary, provided you don't care whether this thread observes
the value that was before the change or the value that is after
the change."
Sorry, I must have misunderstood. I still do. Never mind.
Correct advice is: this subject is too complicated for a simple
answer, go read references provided elsewhere in this thread.
Agreed.
Apart from our little disagreement: do use volatile in your
multithreaded programs. It is important.
That is incorrect. In the absence of signals, you should *not*
use volatile -- doing so unnecessarily slows down your programs,
brings no benefit, and may mask synchronization bugs (if a program
(which does not use signals) "works" with volatile and breaks
without it, then it has a synchronization bug).
Slowdown: of course
Benefit: variable is not stored in registers anymore. Hence, it will be store in the main memory. Hence, the other processor/thread will see all the changes. Did I miss something ?
Synchronization bugs: you suggested no synchronization whatsoever.
I direct you to this thread:
http://groups.google.com/group/comp.programming.threads/browse_frm/thread/9c1a21dd394caf0a
and in particular this message from *the* expert:
http://groups.google.com/group/comp.programming.threads/msg/2ded073b4464b89a
for further info.
Cheers,
An interesting reading. The thread is dated August 2000.
I am not qualified enough to contest the experts, but the messages might be slightly outdated.
Now, to quote you: "The page you are referring to deals with signals,
and is not relevant to current discussion.". Fine. I took the time to translate that page into POSIX threads. No signals.
I will list the program below and explain how I compiled/tested it. If "volatile" has no meaning in the context of threads, please explain me what is happening. I am not being ironical. Please explain, because I don't understand.
Mihai
==========================================================
Compile the code like this:
gcc -O2 wrong.c -pthread -DPTHREADS -o wrong_nv
gcc -O2 -DVOLATILE=volatile -DPTHREADS wrong.c -pthread -o wrong_v
The output of the two programs is:
-------------------------------------
mike@acer:~/work/test$ ./wrong_v
Creating thread
Thread created
Executing thread
Thread finished: 10000
x = 1733793664
main program finished: 10000
mike@acer:~/work/test$
-------------------------------------
mike@acer:~/work/test$ ./wrong_nv
Creating thread
Thread created
Executing thread
Thread finished: 10000
<hangs here, CPU usage 100%>
------------------------------------
The program itself:
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#ifndef VOLATILE
#define VOLATILE
#endif
VOLATILE int total=0;
void *PrintHello(void *threadid)
{
int i;
printf("Executing thread \n");
for(i = 0;i<10000;i++){
total++;
}
printf("Thread finished: %d\n", total);
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t thread;
int rc, t;
VOLATILE unsigned x=0;
VOLATILE int i,j;
printf("Creating thread \n");
pthread_create(&thread, NULL, PrintHello, (void *)t);
printf("Thread created\n");
#define BIGNUM 10000
while (total < 100 )
{
for (i=0; i<BIGNUM; i++)
{
for (j=0; j<BIGNUM; j++)
x = x+j ;
}
}
printf("x = %u\n",x); // so optimizers doesn't throw away the loop
printf("main program finished: %d\n",total);
pthread_exit(NULL);
}
.
- Follow-Ups:
- Re: Thread question
- From: David Schwartz
- Re: Thread question
- From: Paul Pluzhnikov
- Re: Thread question
- References:
- Re: Thread question
- From: Paul Pluzhnikov
- Re: Thread question
- From: Mihai Osian
- Re: Thread question
- From: Paul Pluzhnikov
- Re: Thread question
- Prev by Date: Re: Thread question
- Next by Date: Re: Thread question
- Previous by thread: Re: Thread question
- Next by thread: Re: Thread question
- Index(es):
Relevant Pages
|