Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Multithreading question

Author: Dann Corbit

Date: 23:43:45 09/08/03

Go up one level in this thread


On September 09, 2003 at 02:14:08, Tony Werten wrote:

>Hi,
>
>maybe a silly question, but one get quite desperate during debugging. Suppose I
>have the followin code:
>
>procedure whatever(param:integer)
>begin
>   do_something_heavy_with_param;
>end;
>
>Say, the heavy stuff takes about 10 seconds.
>
>If during these secs another thread is calling this function, what happens ?
>
>Does every thread gets a "local copy" of the function ?
>
>On a single processor, there is a context switch, stuff is pushed on a stack,
>and everything is safe.
>
>What about a dual ? I get the feeling that param is actually changed by the
>second call.

I know the C or C++ answer, but not the answer for Delphi.

In C or C++, all automatic variables and function parameters are pushed onto the
stack (or that must be emulated somehow by the compiler).  So a variable called
foo will be independent for each invocation of the function.

A static or a global variable is not duplicated.  Hence, you cannot access for
writing in more than one thread at a time.  The usual way to solve this problem
is to use a critical section across threads or to use a mutex across processes.

These things are pushed during function invocation:
The function address
The function parameters
The automatic variables (those not declared as static or declared outside the
body of a function).

In C, you won't modify the contents in the calling program unless you push a
pointer to the object.  In C++, it is a pointer or a reference that will allow
modification of the value in the calling function.  Otherwise, it will always be
a copy of the value that is pushed.



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.