The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: Danesh on July 08, 2005, 06:50:13 PM

Title: Again concurrency
Post by: Danesh on July 08, 2005, 06:50:13 PM
Hi all,

Assume you have multi-thread program which has many threads running at the sam time. There is also a function which is called SampleFunc(). This SampleFunc() has many local defined variables too and will be called many times by all threads. Now assume that a thread calls SampleFunc() and of course its local variables (function's local variables) have been changed and at that time another thread calls SampleFunc(). I want to know, does each thread use its own fresh copy on SampleFunc() with its own local variables or this function will be just called for all threads. I mean, if each thread doesn't use its own copy of function it would be awful because all variables which have been defined within function may change and the program would crash immediately. Am I right ?

Regards,

Danesh Daroui

Title: Re: Again concurrency
Post by: Eóin on July 08, 2005, 07:50:09 PM
Local variables are stored on the stack, and each thread has its own stack so there should be no problems once you stay away from global variables.
Title: Re: Again concurrency
Post by: James Ladd on July 08, 2005, 10:55:02 PM
Danesh,
local variables within a thread are safe for you to use and other threads get their own copy.
rgs, striker
Title: Re: Again concurrency
Post by: brixton on July 09, 2005, 11:16:08 AM
Isn't each reference to the function replaced by the function itself at assembly time?  If I'm right, then none of the threads should interfere with one another :green2
Title: Re: Again concurrency
Post by: Mark Jones on July 09, 2005, 01:05:55 PM
I think what Danesh is doing is a little different than "coding functions" in MASM. He's creating multiple threads which all run one procedure. (The difference being, each thread is a separate "program" and therefore does not share the stack and data with each other.) :toothy  However, if each thread accessed a GLOBAL variable and modified it, then that would create a mess.
Title: Re: Again concurrency
Post by: Danesh on July 09, 2005, 06:53:46 PM
Yes Mark is right. I am working on a program which has many threads and each thread may call a function at one time. Thanks for help. I feel better know, because I had tested the program with 5 connections ( I have 5 systems) and could not test it in more critical situations but now I feel better.  :U

Thanks all,

Danesh

Title: Re: Again concurrency
Post by: James Ladd on July 09, 2005, 10:41:34 PM
If you have multiple threads and each thread executes the same function then the local variables of the function are
thread safe. This is because you actually dont have multiple threads actually executing at the same time. The
local data is saved in teh context switch.
If you have multiple threads and each thread execute the same function and that function reads or writes global
data, then you have to protect the read/write with some form of mutex. Like a critical section.