[Insight-developers] thread safety question

Will Schroeder will.schroeder@kitware.com
Fri, 16 Feb 2001 07:18:50 -0500


Hi Lydia-

Here's how we've used threads in the past.

Most every vtk image filter is multithreaded, and it is
completely transparent to the user. When the pipeline
executes, the images is broken into pieces, and a separate
thread operates on each piece. The number of pieces can
be manually controlled, but typically it is automatically
determined based on the number of processors on the
system.

This means filters that use an auxiliary function/class depend
on the auxiliary function to be thread safe. This typically means
that you cannot use ivars in the class as scratch space, if
the functions write to the ivars (unless they are mutex-locked,
which is sloooow).

Also, since we are trying to build a toolkit that allows users to
assemble pieces arbitrarily, we have to be careful how these
auxiliary functions to interact with the rest of the system. If they
assume too much about the invoking class, then it can make it
too hard to use the functions.

Note: you could always set up the auxiliary class/functions to
accept a "structure" as part of the invocation/instantiation. This
structure would serve as the scratch space, and could be allocated
just once per thread.

Hope this helps,
Will


At 10:52 PM 2/15/2001 -0800, Lydia Ng wrote:
>Hi,
>
>I have a couple of questions on the issue of thread safety.
>
>My general questions are:
>- What are our design specifications in terms of thread safety?
>- What is the group consensus in terms of which types of
>   objects need to be thread safe?
>
>I have a more specific question:
>
>Suppose I have a ImageFunction subclass, say FooFunction.
>I have an algorithm which creates an instance of FooFunction
>and calls the Evaluate() method many times (O(10^6)) per iteration.
>Each evaluation requires some scratch variables. Currently, I
>have been using member variables as scratch memory to avoid
>the overhead of allocating and deallocating them each time
>I call Evaluate().
>
>It was brought to my attention that this may not be a good thing
>to do as it not thread safe.
>
>My view is that I see ImageFunction as lightweight utility classes
>like Iterators and I would not expect the same instance of
>FooFunction to be used across threads. Is this thinking wrong?
>
>Lydia
>
>
>_______________________________________________
>Insight-developers mailing list
>Insight-developers@public.kitware.com
>http://public.kitware.com/mailman/listinfo/insight-developers