[Insight-users] multithreaded programming updating a commonvariable

Miller, James V (Research) millerjv at crd.ge.com
Tue May 3 17:41:22 EDT 2005


To followup on Luis's note.

If possible, it is best to avoid updating a common variable.  The associated
synchronization will kill your performance.  

The approach we usually take is have an array of variables where each thread
writes to a single element in the array. In your case a vector of "counts", 
one count for each thread.  Then in the AfterThreadedGenerateData() method
you can combine these counts to the final value.  (BeforeThreadedGenerateData() 
and AfterThreadedGenerateData() run single threaded an provide an opportunity
to initialize data for the threads and to combine data from the threads 
respectively).

Jim



-----Original Message-----
From: insight-users-bounces+millerjv=crd.ge.com at itk.org
[mailto:insight-users-bounces+millerjv=crd.ge.com at itk.org]On Behalf Of
Luis Ibanez
Sent: Tuesday, May 03, 2005 4:21 PM
To: Abhishek
Cc: insight-users at itk.org
Subject: Re: [Insight-users] multithreaded programming updating a
commonvariable



Hi Abhishek,

Yes, ITK provide classes for Mutex locks and Semaphores.

Please look at the following files in Insight/Code/Common:

itkBarrier.h
itkConditionVariable.h
itkFastMutexLock.h
itkLightObject.h
itkMetaDataObject.h
itkMultiThreader.h
itkMutexLock.h
itkMutexLockHolder.h
itkSemaphore.h
itkSimpleFastMutexLock.h


Also, look at the Statistics filter for an example on
how to use these classes for a multi-threaded filter.

      Insight/Code/BasicFilters/
               itkStatisticsImageFilter.txx


In particular you want to look at the methods:

    BeforeThreadedGenerateData();
    ThreadedGenerateData();
    AfterThreadedGenerateData();


Regards,


    Luis


-----------------
Abhishek wrote:
> Hello Everyone,
> 
>  
> 
> I am writing a multithreaded image filter and I want to update a common 
> variable ‘count’. Does ITK provide any macros/ classes for using a 
> mutex/semaphore? Please guide me the best way to go about it and if 
> there is any existing code in ITK I can use as a reference. Thanks,
> 
>  
> 
> -Cheers,
> 
> Abhishek
> 
>  
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk.org
> http://www.itk.org/mailman/listinfo/insight-users



_______________________________________________
Insight-users mailing list
Insight-users at itk.org
http://www.itk.org/mailman/listinfo/insight-users


More information about the Insight-users mailing list