++ functions and threads ++

Is it true that a function is thread safe (can be used by multiple threads without fear of data corruption) so long as it contains no global or static data (all local and passed parameters and variables are addressed through the stack so each call to the function should have its own copy of the parameters and variables).  ??
LVL 1
pcj0961Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jhanceCommented:
That would be a oversimplification in my opinion.

A prime place to look for non-threadsafe programming is in the use of static and global data but there are many other pitfalls.  Access to system resources, files, DLLs, IO devices, etc. can all introduce problems with multi-threaded applications.

I think it would not be too difficult to create an example application that had no static or global variables but still failed miserably..
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pcj0961Author Commented:
yes the way I asked it was an oversimplification.  I guess my
question was more toward user written functions that don't use system resources, files, DLL's etc. .. but ones that are simply written to manipulate passed in data and return status or data
to the calling function (ex. a function that manipulates a string and returns something about or part of the string).  I guess the best approach would be to use synch. objects for all functions
used by multiple threads.
0
ZoppoCommented:
Hi pcj0961,

your right; even `simple` function which only access some data can cause an acces vialotion or a deadlock in some cases when there`s no synchronization ... BTW, even if you`re thinkingthat your app is running without
problems, try it on a multi-processor system

ZOPPO
0
abdijCommented:
Hi,

If your simple thread application has a pointer to a variable as its parameter and it is manipulated in the thread, then again there is a problem.

Its always better to have some semaphore or mutex (synchronization) for the thread manipulated objects.

abdij_b@hotmail.com
0
abdijCommented:
Sorry, a thousan apologies
typo mistake.

If your simple thread application has a function having a pointer to a variable as its parameter and it is manipulated in the function, then again there is a problem.

Its always better to have some semaphore or mutex (synchronization) for the thread manipulated objects.

abdij
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.