Re: mutex question
- From: Tom Widmer <tom_usenet@xxxxxxxxxxx>
- Date: Wed, 10 May 2006 09:57:56 +0100
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
.
- Follow-Ups:
- Re: mutex question
- From: Slava M. Usov
- Re: mutex question
- References:
- Re: mutex question
- From: David Jones
- Re: mutex question
- From: Tom Widmer [VC++ MVP]
- Re: mutex question
- From: Slava M. Usov
- Re: mutex question
- From: Tom Widmer [VC++ MVP]
- Re: mutex question
- From: Slava M. Usov
- Re: mutex question
- From: Tom Widmer [VC++ MVP]
- Re: mutex question
- From: Slava M. Usov
- Re: mutex question
- From: Tom Widmer [VC++ MVP]
- Re: mutex question
- From: Slava M. Usov
- Re: mutex question
- From: Tom Widmer [VC++ MVP]
- Re: mutex question
- From: Slava M. Usov
- Re: mutex question
- From: Tom Widmer [VC++ MVP]
- Re: mutex question
- From: Slava M. Usov
- Re: mutex question
- Prev by Date: Re: mutex question
- Next by Date: Re: Unlink from a Read-Only File
- Previous by thread: Re: mutex question
- Next by thread: Re: mutex question
- Index(es):
Relevant Pages
|
Loading