Xmipp  v3.23.11-Nereus
Public Member Functions | Public Attributes | Friends | List of all members
ThreadManager Class Reference

#include <xmipp_threads.h>

Public Member Functions

void setData (void *data, int nThread=-1)
 
 ThreadManager (int numberOfThreads, void *workClass=NULL)
 
 ~ThreadManager ()
 
void run (ThreadFunction function, void *data=NULL)
 
void runAsync (ThreadFunction function, void *data=NULL)
 
void wait ()
 
int getNumberOfThreads ()
 

Public Attributes

int threads
 number of working threads. More...
 

Friends

void * _threadMain (void *data)
 

Detailed Description

Class for manage a group of threads performing one or several tasks. This class is very useful when we have some function that can be executed in parallel by threads. The threads are created in the contructor of the object and released in destructor. This way threads can execute different functions at diffent moments and exit at the end of manager life. Also, the wait() function allow in the main thread to wait until all threads have finish working on a task and maybe then execute another one. This class is supposed to be used only in the main thread.

Definition at line 239 of file xmipp_threads.h.

Constructor & Destructor Documentation

◆ ThreadManager()

ThreadManager::ThreadManager ( int  numberOfThreads,
void *  workClass = NULL 
)

Constructor, number of working threads should be supplied

Definition at line 205 of file xmipp_threads.cpp.

206 {
207  threads = numberOfThreads;
208  barrier = new Barrier(threads + 1);
209  workFunction = NULL;
210  ids = new pthread_t[threads];
211  arguments = new ThreadArgument[threads];
212  started = false;
213  this->workClass = workClass;
214 }
int threads
number of working threads.

◆ ~ThreadManager()

ThreadManager::~ThreadManager ( )

Destructor, free memory and exit threads

Definition at line 248 of file xmipp_threads.cpp.

249 {
250  //Destroy the threads
251  workFunction = NULL;
252  if (started)
253  {
254  wait();
255  for (int i = 0; i < threads; ++i)
256  pthread_join(ids[i], NULL);
257  }
258 
259  delete barrier;
260  delete[] ids;
261  delete[] arguments;
262 }
int threads
number of working threads.
#define i

Member Function Documentation

◆ getNumberOfThreads()

int ThreadManager::getNumberOfThreads ( )
inline

Get number of threads

Definition at line 328 of file xmipp_threads.h.

328 {return threads;}
int threads
number of working threads.

◆ run()

void ThreadManager::run ( ThreadFunction  function,
void *  data = NULL 
)

Function to start working in a task. The function that you want to execute in parallel by the working threads should be passed as argument. If data is passed, then it is set to all threads. Functions that can be executed by thread should by of the type ThreadFunction, i.e., return void * and only one argument of type ThreadArgument. The call of this function will block the main thread until all workers finish their job, if you don't want to block use runAsync instead, and later can call wait for waiting until threads are done.

//Global variables, so it are visible in 'processSeveralImages()'
//function to perform some operation
//to N images executed in parellel
void * processImages(ThreadArgument & data)
{
int thread_id = arg.thread_id;
size_t firstImage, lastImage;
while (td->getTasks(firstImage, lastImage))
for (int image = firstImage; image <= lastImage; ++image)
{
//...
processOneImage(image);
//...
}
}
int main()
{
//...
//Distribute 1000 tasks in blocks of 100.
td = new ThreadTaskDistributor(1000, 100);
//Start 2 threads to work on it
tm.run(processImages);
//...
//Same threads can work in other function
tm.run(processVolumes);
}

Definition at line 264 of file xmipp_threads.cpp.

265 {
266  runAsync(function, data);
267  //Wait on barrier to wait for threads finish
268  wait();
269 }
void runAsync(ThreadFunction function, void *data=NULL)

◆ runAsync()

void ThreadManager::runAsync ( ThreadFunction  function,
void *  data = NULL 
)

Same as run but without blocking.

Definition at line 271 of file xmipp_threads.cpp.

272 {
273  if (data != NULL)
274  setData(data);
275  workFunction = function;
276  if (!started)
277  createThreads();
278  //Wait on barrier to threads starts working
279  wait();
280 }
void setData(void *data, int nThread=-1)

◆ setData()

void ThreadManager::setData ( void *  data,
int  nThread = -1 
)

Set data for working threads. If nThread = -1 then data is set for all threads.

Definition at line 216 of file xmipp_threads.cpp.

217 {
218  if (idxThread == -1)
219  for (int i = 0; i < threads; ++i)
220  arguments[i].data = data;
221  else
222  arguments[idxThread].data = data;
223 }
int threads
number of working threads.
#define i

◆ wait()

void ThreadManager::wait ( )

Function that should be called to wait until all threads finished work

Definition at line 282 of file xmipp_threads.cpp.

283 {
284  barrier->wait();
285 }
void wait()

Friends And Related Function Documentation

◆ _threadMain

void* _threadMain ( void *  data)
friend

function to start running the threads. Should be external and declared as friend

This function is used in ThreadManager as the main threads function to live in.

Definition at line 173 of file xmipp_threads.cpp.

174 {
175  ThreadArgument * thArg = (ThreadArgument*) data;
176  ThreadManager * thMgr = thArg->manager;
177 
178  while (true)
179  {
180  //Wait for start working or leave
181  thMgr->wait();
182  //After awaked check what to do
183  if (thMgr->workFunction != NULL)
184  {
185  try
186  {
187  thMgr->workFunction(*thArg);
188  thMgr->wait(); //wait for finish together
189  }
190  catch (XmippError &xe)
191  {
192  std::cerr << xe.what() << std::endl
193  << "In thread " << thArg->thread_id << std::endl;
194 // pthread_exit(NULL);
195  exit(xe.__errno);
196  }
197  }
198  else //exit thread
199  {
200  pthread_exit(NULL);
201  }
202  }
203 }
ThreadManager * thMgr
int thread_id
The thread id.
ErrorType __errno
Definition: xmipp_error.h:227

Member Data Documentation

◆ threads

int ThreadManager::threads

number of working threads.

Definition at line 242 of file xmipp_threads.h.


The documentation for this class was generated from the following files: