News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Again concurrency

Started by Danesh, July 08, 2005, 06:50:13 PM

Previous topic - Next topic

Danesh

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


Eóin

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.

James Ladd

Danesh,
local variables within a thread are safe for you to use and other threads get their own copy.
rgs, striker

brixton

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
If you love somebody, set them free.
If they return, they were always yours. If they don't, they never were..

Mark Jones

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.
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

Danesh

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


James Ladd

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.