TThread, Terminate, Boolean, Alignment ???

TThread, Terminate, Boolean, Alignment ???

Post by Skybuck Fl » Wed, 15 Oct 2008 19:19:39


Hello,

In theory TThread.FTerminated boolean would not need to be protected from
synchronization, since it's only one field/boolean which would be atomically
accessed/write/read.

However in practice I have seen this theory "fail".

I believe a critical section is needed around the boolean to ensure correct
function for code like:

// inside thread:
while not Terminated do
begin

end;


// outside thread
Terminate;
WaitFor;
Free;

etc.

Any idea's why this code would fail with just the boolean approach and no
critical sections for it ???

Bye,
Skybuck.
 
 
 

TThread, Terminate, Boolean, Alignment ???

Post by Szabolcs F » Wed, 15 Oct 2008 19:50:11


The reason is that both compilers and the processor hardware are mad
about optimisation, which traditionally means tailoring our SW for the
sequential execution of programs. In your case either the compiler or
the hardware was `intelligent' and overruled your intention. That
means that the change in the status of the variable is not propagated
which is an optimisation in sequential programs.

On the other hand, using critical sections both the compiler and the
hardware is informed that it is a piece of shared variable so this
`optimisation' does not happen.

Best Regards,
Szabolcs

 
 
 

TThread, Terminate, Boolean, Alignment ???

Post by David Schw » Wed, 15 Oct 2008 20:49:57


Atomicity is not relevant. This isn't an atomicity issue.


What? You've seen a boolean read or write fail to be atomic?


It depends upon the language and coding standard. If this is pseudo-
code intended to represent C/POSIX code, then the standard says that
you get unpredictable results if one thread can modify a variable
while another thread is or might be accessing it. So the standard says
this doesn't have to work and, not surprisingly, it doesn't.

DS
 
 
 

TThread, Terminate, Boolean, Alignment ???

Post by Skybuck Fl » Thu, 16 Oct 2008 02:30:38

It's amazing how my brain manages to solve problems while I am asleep.

"Yesterday" I was tired and I didn't see the problem.

"Today" I wake up and immediatly understand the problem.

At least I think so...

Problem description:

There are two threads:

ThreadMain
ThreadTest

The code which they execute is
(which I will put side-by-side, for illustration):


ThreadMain ThreadTest
begin begin
ThreadTest.Terminate; while not Terminated do
ThreadTest.WaitFor; begin
ThreadTest.Free; end;
end; end;

The order in which these statements are executed is probably what is causing
the problem in rare cases.

The rare case would be:

1. ThreadMain ThreadTest.Terminate;
2. ThreadTest.while not Terminated do
3. ThreadMain ThreadTest.WaitFor;

So you see the problem now ?

I do, the problem is:

First ThreadMain tells ThreadTest to terminate, then ThreadTest immediatly
terminates.

Then ThreadMain tries to wait for ThreadTest to terminate... but it already
terminated ?!

So ThreadMain waits forever on ThreadTest to terminate !

That's why it can never work ?!

So if I am correct about this then CodeGear has had a bug in there thread
code up to this day....

Which is about 10 years or so ?!

This bug is still in Delphi 2009 ?! WOW.

Which more or less proves how difficult it can be to find/detect bugs.

^^^ Probably caused by their bad testing as well ;) :) ^^^

I solved this with a critical section for the terminated boolean...

Maybe there is a better/more efficient solution ???

On a side note:

I left my computer running the whole "night" because it's downloading
Brother in Arms: Hells highway :)

Also Call of Duty 5 will come out 8 days from now ? So that gonna be fun too
lol. Gonna be busy *** weeks :)

Anyway something happened to my computer when I was a sleep and downloading
with BitComet 0.63 or so:

BitComet is still working... but Explorer.EXE crashes with an unknown
exception... probably in the .NET section or so ;)

I hope my computer didn't get hacked or infected ;) Maybe it was just a
failed hack attempt but this shows Windows X64 still has some serious bugs
here and there... (Explorer.exe hangs sometimes too...)

I restarted explorer.exe so now everything is fine...

Today Microsoft is probably gonna patch some holes... I always wait a
little bit with patching the operating system with new patches... One never
knows how that works out... sometimes people have problems with new patches
so that's why I always wait a few days or weeks... to know if the patches
work ok or not ok ;) :)

Bye,
Skybuck ! =D
 
 
 

TThread, Terminate, Boolean, Alignment ???

Post by Skybuck Fl » Thu, 16 Oct 2008 02:33:26

Nice try but that ain't the problem.

The boolean is garantueed to be "atomic" by intel documents so both threads
see it in the same state, so propagating the state is not the problem.

The problem is with the order in which statements are executed.

See my other sub thread for an explanation what is going wrong ;)

Bye,
Skybuck.
 
 
 

TThread, Terminate, Boolean, Alignment ???

Post by Skybuck Fl » Thu, 16 Oct 2008 02:36:00

What the *** ?!?!

Outlook express completely messed up the last message ?!?!

Wow what the *** ?!?

Let's try that again:

It's amazing how my brain manages to solve problems while I am asleep.

"Yesterday" I was tired and I didn't see the problem.

"Today" I wake up and immediatly understand the problem.

At least I think so...

Problem description:

There are two threads:

ThreadMain
ThreadTest

The code which they execute is
(which I will put side-by-side, for illustration):


ThreadMain ThreadTest
begin begin
ThreadTest.Terminate; while not Terminated do
ThreadTest.WaitFor; begin
ThreadTest.Free; end;
end; end;

The order in which these statements are executed is probably what is causing
the problem in rare cases.

The rare case would be:

1. ThreadMain ThreadTest.Terminate;
2. ThreadTest.while not Terminated do
3. ThreadMain ThreadTest.WaitFor;

So you see the problem now ?

I do, the problem is:

First ThreadMain tells ThreadTest to terminate, then ThreadTest immediatly
terminates.

Then ThreadMain tries to wait for ThreadTest to terminate... but it already
terminated ?!

So ThreadMain waits forever on ThreadTest to terminate !

That's why it can never work ?!

So if I am correct about this then CodeGear has had a bug in there thread
code up to this day....

Which is about 10 years or so ?!

This bug is still in Delphi 2009 ?! WOW.

Which more or less proves how difficult it can be to find/detect bugs.

^^^ Probably caused by their bad testing as well ;) :) ^^^

I solved this with a critical section for the terminated boolean...

Maybe there is a better/more efficient solution ???

On a side note:

I left my computer running the whole "night" because it's downloading
Brother in Arms: Hells highway :)

Also Call of Duty 5 will come out 8 days from now ? So that gonna be fun too
lol. Gonna be busy *** weeks :)

Anyway something happened to my computer when I was a sleep and downloading
with BitComet 0.63 or so:

BitComet is still working... but Explorer.EXE crashes with an unknown
exception... probably in the .NET section or so ;)

I hope my computer didn't get hacked or infected ;) Maybe it was just a
failed hack attempt but this shows Windows X64 still has some serious bugs
here and there... (Explorer.exe hangs sometimes too...)

I restarted explorer.exe so now everything is fine...

Today Microsoft is probably gonna patch some holes... I always wait a
little bit with patching the operating system with new patches... One never
knows how that works out... sometimes people have problems with new patches
so that's why I always wait a few days or weeks... to know if the patches
work ok or not ok ;) :)

Bye,
Skybuck ! =D
 
 
 

TThread, Terminate, Boolean, Alignment ???

Post by Skybuck Fl » Thu, 16 Oct 2008 02:38:46

Holyshit !

I just found another bug... GOD DAMN IT.

I am a *** ing BUG MAGNET...

And you people are *** ING LAZY and really STUPID/RETARDED.

*** ING TEST YOUR CODE YOU SONS OF *** ES !

Anyway... now I have to alter the message to *** ing post it properly...

(Outlook express makes an attachment of begin begin or something)

It's amazing how my brain manages to solve problems while I am asleep.

"Yesterday" I was tired and I didn't see the problem.

"Today" I wake up and immediatly understand the problem.

At least I think so...

Problem description:

There are two threads:

ThreadMain
ThreadTest

The code which they execute is
(which I will put side-by-side, for illustration):

// Outlook express bug was here:
// If I put these two pieces of code side by side like so:

ThreadMain line 1 ThreadTest line 2 it makes an attachment of it oh my god
stupid !

ThreadMain
begin
ThreadTest.Terminate;
ThreadTest.WaitFor;
end;

ThreadTest
begin
while not Terminated do
begin
ThreadTest.Free;
end;
end;


The order in which these statements are executed is probably what is causing
the problem in rare cases.

The rare case would be:

1. ThreadMain ThreadTest.Terminate;
2. ThreadTest.while not Terminated do
3. ThreadMain ThreadTest.WaitFor;

So you see the problem now ?

I do, the problem is:

First ThreadMain tells ThreadTest to terminate, then ThreadTest immediatly
terminates.

Then ThreadMain tries to wait for ThreadTest to terminate... but it already
terminated ?!

So ThreadMain waits forever on ThreadTest to terminate !

That's why it can never work ?!

So if I am correct about this then CodeGear has had a bug in there thread
code up to this day....

Which is about 10 years or so ?!

This bug is still in Delphi 2009 ?! WOW.

Which more or less proves how difficult it can be to find/detect bugs.

^^^ Probably caused by their bad testing as well ;) :) ^^^

I solved this with a critical section for the terminated boolean...

Maybe there is a better/more efficient solution ???

On a side note:

I left my computer running the whole "night" because it's downloading
Brother in Arms: Hells highway :)

Also Call of Duty 5 will come out 8 days from now ? So that gonna be fun too
lol. Gonna be busy *** weeks :)

Anyway something happened to my computer when I was a sleep and downloading
with BitComet 0.63 or so:

BitComet is still working... but Explorer.EXE crashes with an unknown
exception... probably in the .NET section or so ;)

I hope my computer didn't get hacked or infected ;) Maybe it was just a
failed hack attempt but this shows Windows X64 still has some serious bugs
here and there... (Explorer.exe hangs sometimes too...)

I restarted explorer.exe so now everything is fine...

Today Microsoft is probably gonna patch some holes... I always wait a
little bit with patching the operating system with new patches... One never
knows how that works out... sometimes people have problems with new patches
so that's why I always wait a few days or weeks... to know if the patches
work ok or not ok ;) :)

Bye,
Skybuck ! =D
 
 
 

TThread, Terminate, Boolean, Alignment ???

Post by Skybuck Fl » Thu, 16 Oct 2008 02:40:44

Damn I *** that posting up a little bit...

Here it is one more time:

Holyshit !

I just found another bug... GOD DAMN IT.

I am a *** ing BUG MAGNET...

And you people are *** ING LAZY and really STUPID/RETARDED.

*** ING TEST YOUR CODE YOU SONS OF *** ES !

Anyway... now I have to alter the message to *** ing post it properly...

(Outlook express makes an attachment of begin begin or something)

It's amazing how my brain manages to solve problems while I am asleep.

"Yesterday" I was tired and I didn't see the problem.

"Today" I wake up and immediatly understand the problem.

At least I think so...

Problem description:

There are two threads:

ThreadMain
ThreadTest

The code which they execute is
(which I will put side-by-side, for illustration):

// Outlook express bug was here:
// If I put these two pieces of code side by side like so:

ThreadMain line 1 ThreadTest line 1 it makes an attachment of it oh my god
stupid !

ThreadMain
begin
ThreadTest.Terminate;
ThreadTest.WaitFor;
ThreadTest.Free;
end;

ThreadTest
begin
while not Terminated do
begin
end;
end;


The order in which these statements are executed is probably what is causing
the problem in rare cases.

The rare case would be:

1. ThreadMain ThreadTest.Terminate;
2. ThreadTest.while not Terminated do
3. ThreadMain ThreadTest.WaitFor;

So you see the problem now ?

I do, the problem is:

First ThreadMain tells ThreadTest to terminate, then ThreadTest immediatly
terminates.

Then ThreadMain tries to wait for ThreadTest to terminate... but it already
terminated ?!

So ThreadMain waits forever on ThreadTest to terminate !

That's why it can never work ?!

So if I am correct about this then CodeGear has had a bug in there thread
code up to this day....

Which is about 10 years or so ?!

This bug is still in Delphi 2009 ?! WOW.

Which more or less proves how difficult it can be to find/detect bugs.

^^^ Probably caused by their bad testing as well ;) :) ^^^

I solved this with a critical section for the terminated boolean...

Maybe there is a better/more efficient solution ???

On a side note:

I left my computer running the whole "night" because it's downloading
Brother in Arms: Hells highway :)

Also Call of Duty 5 will come out 8 days from now ? So that gonna be fun too
lol. Gonna be busy *** weeks :)

Anyway something happened to my computer when I was a sleep and downloading
with BitComet 0.63 or so:

BitComet is still working... but Explorer.EXE crashes with an unknown
exception... probably in the .NET section or so ;)

I hope my computer didn't get hacked or infected ;) Maybe it was just a
failed hack attempt but this shows Windows X64 still has some serious bugs
here and there... (Explorer.exe hangs sometimes too...)

I restarted explorer.exe so now everything is fine...

Today Microsoft is probably gonna patch some holes... I always wait a
little bit with patching the operating system with new patches... One never
knows how that works out... sometimes people have problems with new patches
so that's why I always wait a few days or weeks... to know if the patches
work ok or not ok ;) :)

Bye,
Skybuck ! =D
 
 
 

TThread, Terminate, Boolean, Alignment ???

Post by Skybuck Fl » Thu, 16 Oct 2008 02:51:22

Ok,

Let's test this *** ed up outlook express theory because I cannot find
anything in the crappy amateur RFC's.

The code which I will post below is:
begin
begin
Hello!

The begins will be on one line... if I am correct outlook express will ***
it up and you will never see the begin and Hello !

Here goes:

begin begin
Hello !

Bye,
Skybuck.
 
 
 

TThread, Terminate, Boolean, Alignment ???

Post by Skybuck Fl » Thu, 16 Oct 2008 02:51:56

Ok,

That didn't work... maybe it needs a tab character.

Here goes:

begin begin
Hello !

Bye,
Skybuck.
 
 
 

TThread, Terminate, Boolean, Alignment ???

Post by Skybuck Fl » Thu, 16 Oct 2008 02:57:37

Yup that causes the bug !

Lol... the RFC people are such *** ing *** ers !

They can even't design a simply messaging system ! LOL.

Use binary formats or die for god sakes ! ;)

And if you have problems getting the endianess correct then you should die
as well before you start making specifications ! ;) :)

And if you cant convert ASCII, UTF or EBSDIC or whatever then you should die
as well before making any specifications ! LOL.

Oh well at least these *** ers made some crappy "best effort" specifications
so we could use their buggy software and designs for early communications...
because the designing flawless systems would apperently take to long for
them at least ! ;) :) hahahahahaahahahahahahahahahaha.

Big *** ing deal... and big *** ing whooopie ! ;) :)

So one last time the description of the outlook express problem:

begin tabcharacter begin

*** s up everything from that line and down !

WOWWWWWWW SHHHIITTTT ;)

And don't tell me the READ *** ING BUGGY SPECIFICATIONS LIKE THAT ?! :)

Actually please do LOL.

WHERE IS IT ?! I CANT FIND IT !

Never mind... I don't want to read it anymore... in the future I might make
my own SUPERIOR P2P systems.

And don't tell it won't be superior BECAUSE IT WILL BE SUPERIOR.

Just because it will not have all these system mother *** ing bugs in them !
;) :)

Bye,
Skybuck.
 
 
 

TThread, Terminate, Boolean, Alignment ???

Post by Skybuck Fl » Thu, 16 Oct 2008 03:55:38

Hmmm...

Now that I think about it some more...

Maybe the critical section is just "masking" the bug...

Maybe the bug is still present but it just is less likely to occur...

Or maybe the critical section does solve it somehow.

Maybe it's just a logical unsolvable bug/problem ?

Or maybe it can be solved but needs something special... ;)

Hmmmmmm.

Bye,
Skybuck.
 
 
 

TThread, Terminate, Boolean, Alignment ???

Post by Rob Kenned » Thu, 16 Oct 2008 04:12:10


It does no such thing. Go look at the source code. It only takes a few
seconds. TThread.Terminate's implementation is very simple, and it has
never changed since it was introduced in Delphi 2.


No.

It's easy enough to test your instruction-ordering idea. Use the
de *** to control which thread executes at each line.

--
Rob
 
 
 

TThread, Terminate, Boolean, Alignment ???

Post by Skybuck Fl » Thu, 16 Oct 2008 04:58:09

Ok,

Now I am not so sure anymore..

However there is a problem in there somewhere...

Maybe it only happens where there are many threads ! ;)

Or under certain conditions with apc's or so...

Or maybe it's related to the Assert/Exception bug in constructors.

Question is:

What happens if an exception is raised inside Destructors, or Execute
methods.

Could that somehow hang threads as well ? ;)

Hmmm.

Bye,
Skybuck.
 
 
 

TThread, Terminate, Boolean, Alignment ???

Post by Chris M. T » Thu, 16 Oct 2008 05:50:52


For further context, Skybuck Flying is a known troll with severe social
problems; don't waste your time, just plonk it.

[...]