Re: Thread question



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);
}
.



Relevant Pages

  • Re: Named shared memory without synchronization
    ... Since this is an int, my assumption was that this scenario would not benefit ... As you already figured out `volatile' specifier is required in order to tell the compiler that it should not assume any optimizations regarding a variable. ... Alignment is important because unaligned variable may require more than one instruction to access it, therefore breaching atimicity of an access. ... can ever be validated with testing when dealing with synchronization issues. ...
    (microsoft.public.vc.language)
  • [git patches] net driver updates #2
    ... struct sk_buff *skb, int ring_offset) ... +static int use_mcs; ... volatile u8 RCR; ...
    (Linux-Kernel)
  • Re: Is the following code MT-Safe?
    ... the assert (which is a fundamental design error: ... and almost always leads to either major synchronization failures ... >Does _bRunning need to be tagged as volatile? ... compiler to cache values, but only during the execution of a function; ...
    (microsoft.public.vc.mfc)
  • Re: Two more multithreading questions
    ... I want to know if making the variable volatile will guarantee that the second thread always sees the latest integer created by the first thread. ... is that when one thread executes assignment statements to volatile ... It is possible that one thread starts executing an assignment statement ... JLS 17.4.4 Synchronization Order ...
    (comp.lang.java.programmer)
  • Re: Share .cpp and .h along projects
    ... pvector = InterlockedExchangePointerAcquire(&g_sharedVector, ... I really would like to hear what you think volatile accomplishes ... You can't require people to use volatile on top of synchronization. ... compiler useful for multithreaded programming. ...
    (microsoft.public.vc.language)