1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
/*
-- MAGMA (version 2.9.0) --
Univ. of Tennessee, Knoxville
Univ. of California, Berkeley
Univ. of Colorado, Denver
@date January 2025
@author Mark Gates
*/
#ifndef MAGMA_THREAD_HPP
#define MAGMA_THREAD_HPP
#include <queue>
#include "magma_internal.h"
/******************************************************************************/
extern "C"
void* magma_thread_main( void* arg );
/***************************************************************************//**
Super class for tasks used with \ref magma_thread_queue.
Each task should sub-class this and implement the run() method.
@ingroup magma_thread
*******************************************************************************/
class magma_task
{
public:
magma_task() {}
virtual ~magma_task() {}
virtual void run() = 0; // pure virtual function to execute task
};
/******************************************************************************/
class magma_thread_queue
{
public:
magma_thread_queue();
~magma_thread_queue();
void launch( magma_int_t in_nthread );
void push_task( magma_task* task );
void sync();
void quit();
protected:
friend void* magma_thread_main( void* arg );
magma_task* pop_task();
void task_done();
magma_int_t get_thread_index( pthread_t thread ) const;
private:
std::queue< magma_task* > q; ///< queue of tasks
bool quit_flag; ///< quit() sets this to true; after this, pop returns NULL
magma_int_t ntask; ///< number of unfinished tasks (in queue or currently executing)
pthread_mutex_t mutex; ///< mutex lock for queue, quit, ntask
pthread_cond_t cond; ///< condition variable for changes to queue and quit (see push, pop, quit)
pthread_cond_t cond_ntask; ///< condition variable for changes to ntask (see sync, task_done)
pthread_t* threads; ///< array of threads
magma_int_t nthread; ///< number of threads
};
#endif // #ifndef MAGMA_THREAD_HPP
|