<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
Thanks Sven, I really appreciate.<br>
<br>
Le 23/03/2012 10:59, Sven Barth a écrit :
<blockquote cite="mid:4F6C4970.2090702@googlemail.com" type="cite">Am
23.03.2012 10:25, schrieb Antonio Fortuny:
<br>
<blockquote type="cite">Le 22/03/2012 16:20, Mattias Gaertner a
écrit :
<br>
<blockquote type="cite">
<br>
Each thread has its own stack.
<br>
<br>
All threads share the same address space and the same heap.
<br>
<br>
Objects (here: class instances) are created on the heap. The
heap is
<br>
thread safe.
<br>
<br>
The data segment is for constants. They are shared.
<br>
<br>
</blockquote>
Le 22/03/2012 17:10, Michael Schnell a écrit :
<br>
<blockquote type="cite">Besides what the other said a very basic
comment.
<br>
<br>
The location where an object is defined (i.e. within a TThread
enabled
<br>
unit) or who created it (the main line code or the thread
code) does
<br>
not matter. The Concept of classes, objects and instances is a
matter
<br>
of memory allocation and pointers and not a concept of program
flow.
<br>
same is absolutely independent. You can use one instance of a
class in
<br>
one thread and another one in another thread. You can create
an
<br>
instance in one thread and call its procedures and properties
by
<br>
another one. (BTW this results in the fact that its very hard
to
<br>
define something like "Thread-safe" for a class).
<br>
</blockquote>
Thanks to you all .
<br>
Trying to be as much clear as possible, there are some
sentences:
<br>
<br>
Assuming this few statements (aka my own rules when writing
thread's code):
<br>
a. all thread code+data are encapsulated into a TThread object
<br>
b. "Thread safe" means that there is no overlap of data
references from
<br>
thread to thread, the main thread inclusive, and that no
references are
<br>
passed outside the thread's control from thread to thread (by
the means
<br>
of the owner or global vars for instance)
<br>
c. the thread creator does not interfere in any way with the
thread's
<br>
data and methods as soon as the thread has been started
(Terminated
<br>
execpted of course)
<br>
d. no function neither procedure calls are made to any function
or
<br>
procedure defined outside of the thread control (aka self
defined methods)
<br>
</blockquote>
<br>
Ok, I hereby assume the above as given.
<br>
<br>
<blockquote type="cite">Do you all agree on the following asserts:
<br>
1. All variables in the thread definition (TThread's private,
protected
<br>
and public variables) are "Thread safe" BUT are accessible to
the thread
<br>
creator
<br>
</blockquote>
<br>
Yes (though the private vars aren't available to the thread
creator if the creator and the definition of your TThread
descendant reside in different units ;) )
<br>
</blockquote>
<blockquote cite="mid:4F6C4970.2090702@googlemail.com" type="cite">
<br>
<blockquote type="cite">2. an instance of an object (aka TObject
descendant) created into the
<br>
thread's EXECUTE procedure is invisible to all other instances
of the
<br>
same object whichever the creator could be (the same thread or
other
<br>
threads created with the same thread definition object) and to
other
<br>
thread object instances, even when the reference variable of the
created
<br>
object is defined into the thread vars (see 1.) provided that
all object
<br>
methods do not call any function or procedure outside of the
object methods.
<br>
</blockquote>
<br>
If I've understood that correctly: yes
<br>
<br>
<blockquote type="cite">3. all variables described in the VAR part
of the EXECUTE procedure are
<br>
"Thread safe" (seems obvious)
<br>
</blockquote>
<br>
Yes
<br>
<br>
<blockquote type="cite">4. all local variables and constants
defined into local Thread object
<br>
methods are "Thread safe" (they are defined into each thread
stack for
<br>
the vars and the heap for constants)
<br>
</blockquote>
<br>
It's true for variables. Local constants are defined in a section
of the executable, so if you have writable constants enabled (only
then it's a problem) and you write to these constants then the
change will be reflected in other constants as well. If you don't
write to the constants than they are safe.
<br>
</blockquote>
Well, when I define a <b>const</b> anywhere in a program it is a <b>constant</b>
actually otherwise I use a var. In this case I guess than there is
no problem for any thread to read a constant whatever the context
could be<br>
<blockquote cite="mid:4F6C4970.2090702@googlemail.com" type="cite">
<br>
<blockquote type="cite">5. all useful code a thread needs should
be encapsultated into a TObject
<br>
descendant and instantiated within the thread's space.
<br>
</blockquote>
<br>
Note necessarily. You can also call global procedures/functions
that don't rely on global state (e.g. IntToStr, etc.). If you want
to call functions/procedures that rely on the state (e.g. some
registration systems for classes) you'll need to synchronize the
access.
<br>
</blockquote>
Is it more accurate to state that any global procedure or function
could be called by any thread as far as the called code does not
reference anything outside itself apart from other global code
considered as "Tread safe" too or constants. For example
StrToIntDef.<br>
<blockquote cite="mid:4F6C4970.2090702@googlemail.com" type="cite">
<br>
<blockquote type="cite">6. all methods defined in the thread's
definition, aprat from the
<br>
EXECUTE procedure (obvious !), run into the thread creator space
(the
<br>
one which instantiates the thread, aka does something like
wThread :=
<br>
TMyThread.Create (False) )
<br>
</blockquote>
<br>
I don't know whether I understood you correctly, but if you have
this:
<br>
<br>
=== example begin ===
<br>
<br>
type
<br>
TTestThread = class(TThread)
<br>
protected
<br>
procedure Execute; override;
<br>
public
<br>
procedure DoSomething;
<br>
end;
<br>
<br>
procedure TTestThread.Execute;
<br>
begin
<br>
DoSomething;
<br>
end;
<br>
<br>
procedure TTestThread.DoSomething;
<br>
begin
<br>
Writeln('Something');
<br>
end;
<br>
<br>
begin
<br>
with TTestThread.Create(True) do begin
<br>
FreeOnTerminate := True;
<br>
Start;
<br>
end;
<br>
end.
<br>
<br>
=== example end ===
<br>
<br>
Then (to my understandment of your assertion) your assertion is
wrong, because DoSomething is (although it is public) only called
in context of Execute (Note: Not that you should make such a
method public if you don't need to, but this is merely an
example).
<br>
</blockquote>
Very nice indeed. You did understand, really.<br>
<blockquote cite="mid:4F6C4970.2090702@googlemail.com" type="cite">
<br>
Regards,
<br>
Sven
<br>
<br>
--
<br>
_______________________________________________
<br>
Lazarus mailing list
<br>
<a class="moz-txt-link-abbreviated" href="mailto:Lazarus@lists.lazarus.freepascal.org">Lazarus@lists.lazarus.freepascal.org</a>
<br>
<a class="moz-txt-link-freetext" href="http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus">http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus</a>
<br>
<br>
</blockquote>
</body>
</html>