Re: Newbie: Calling a function and thread safety

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Scott McPhillips [MVP] (org-dot-mvps-at-scottmcp)
Date: 09/24/04


Date: Fri, 24 Sep 2004 18:48:27 -0500

Harry wrote:

> Hi,
>
> Any local classes and member variables I create within my thread class are
> thread-safe right?
>
> But, I want to call some "utility functions" contained within separate
> header/cpp files that are not within any class and can be called from
> several threads.
>
> eg
> utility.h
> int plus(int *p1, int *p2);
>
> utility.cpp
> int plus(int *p1, int *p2)
> {
> return(*p1+*p2);
> }
>
> What must I do to ensure thread safety?
>
> Thanks

Any function is thread safe if it (and all functions it calls) access
only the parameters passed to it. I.e., does not access any 'outside'
data such as globals.

This overstates the case: The real requirement is that it not access any
data that might be accessed by multiple threads. But this is harder to
verify by simple inspection of the function.

-- 
Scott McPhillips [VC++ MVP]