Re: mutex question



Slava M. Usov wrote:
"Tom Widmer [VC++ MVP]" <tom_usenet@xxxxxxxxxxx> wrote in message
news:uAKjiq3cGHA.4428@xxxxxxxxxxxxxxxxxxxxxxx

Slava M. Usov wrote:


[...]


Reads and writes did happen; your "corrupt" involves an ad hoc
assumption -- the atomicity of reads and writes.

It's neither ad hoc nor an assumption, but a requirement of "sufficient".


Atomicity is a requirement for reading or writing?

Yes. What's your definition of "sufficient" in the context of this thread?

> This is not even funny.

I am finding it increasingly so!

Semantic arguments about the exact meaning of "useful" and "useless"
aside, what is the use of volatile in multithreading?


The 'volatile' keyword is useful with or without multithreading. For a
particular example involving multithreading, read the first message you
responded to in this thread.

That program didn't do anything useful. Nor was it even strictly correct (once volatile is added) due to its use of printf in a thread created by CreateThread (though I'm not sure that is strictly incorrect). Turn it into a program that does something useful, and the code will probably cease to be correct. In any case, I believe it can be written more efficiently (without getting rid of the spin lock) as:

#include <windows.h>
#include <stdio.h>
#include <conio.h>

//volatile not needed
int continue_flag = 1; //global variable

DWORD WINAPI ThreadProc(LPVOID lpVoid)
{
continue_flag = 0;
_WriteBarrier();
return 0;
}

void main(void)
{
continue_flag = 1;
CreateThread(NULL, 0, ThreadProc, 0, 0, 0);
while (continue_flag)
{
_ReadBarrier();
};

printf("Bill: Oh, I'm too! Now, you can press any key to exit\n");
getch();
}

Tom
.



Relevant Pages

  • Re: mutex question
    ... Atomicity is a requirement for reading or writing? ... The 'volatile' keyword is useful with or without multithreading. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Is "volatile sig_atomic_t" redundant?
    ... Dave Vandervies wrote: ... >>which kind of atomicity he had in mind. ... require that declaring an object as just 'sig_atomic_t' (no 'volatile') ...
    (comp.lang.c)
  • Re: mutex question
    ... The 'volatile' keyword is useful with or without multithreading. ... and the very idea of synchronization is to make sure shared ... modifier may be quite usefull for multithreaded program. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Volatile + multithreading
    ... The volatile keyword has little use in multithreaded programming. ... > field or global seems to have no impact because it seems the compiler will ... operations as "special" and suppress optimizations around them. ... > majority of the multithreading issues that could have resulted from ...
    (microsoft.public.vc.language)
  • Re: Thread safety and atomic assignment (again)
    ... By making the internal field volatile, you have made your class data- ... Are the following code snippets thread safe and why? ... Atomicity and volatileness are two different issues: ... guarantee both visibility and atomicity; ...
    (comp.lang.java.programmer)

Loading