Re: Question about threads which share a function



On 6/2/2010 8:47 AM, Joe wrote:
I have a function defined as void foo(int* pIn, int* pOut). The function
gets some data via pointer pIn and calculates some output data which is
written to the array pointed to by pOut. In the calculation of the
output, some local variables defined in foo are used.

If I have two or more threads executing and these threads use foo, are
there any risks
that something might go wrong; like the output being screwed up and
deviating from what is expected?
The danger exists only you use static data, or if pIn/pOut of one invocation overlap with pIn or pOut of another (in which case, which thread's input/output wins over whose).

If you know that none of the pIn and pOut are ever overlapping, and you know that you aren't using global data, you should be fine.

For example, strtok actually uses a static variable to keep track of state, so it is *not* thread-safe. strcpy on the other hand, is (assuming you're not passing in shared char *'s)

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
.