38 pthread_mutex_init(&mutex, NULL);
43 pthread_mutex_destroy(&mutex);
48 pthread_mutex_lock(&mutex);
53 pthread_mutex_unlock(&mutex);
61 pthread_cond_init(&cond, NULL);
67 pthread_cond_destroy(&cond);
82 pthread_cond_wait(&cond, &(mutex->mutex));
87 pthread_cond_signal(&cond);
92 pthread_cond_broadcast(&cond);
99 needed = numberOfThreads;
113 if (called == needed)
116 condition->broadcast();
132 pthread_join(thId, NULL);
141 std::cerr <<
"Thread: can't start thread." << std::endl;
165 this->thread_id = id;
166 this->threads = manager->
threads;
167 this->manager = manager;
169 this->workClass = NULL;
183 if (thMgr->workFunction != NULL)
187 thMgr->workFunction(*thArg);
192 std::cerr << xe.what() << std::endl
193 <<
"In thread " << thArg->
thread_id << std::endl;
207 threads = numberOfThreads;
210 ids =
new pthread_t[threads];
213 this->workClass = workClass;
219 for (
int i = 0;
i < threads; ++
i)
220 arguments[
i].data = data;
222 arguments[idxThread].data = data;
225 void ThreadManager::createThreads()
230 for (
int i = 0;
i < threads; ++
i)
232 arguments[
i].thread_id =
i;
233 arguments[
i].threads = threads;
234 arguments[
i].manager =
this;
235 arguments[
i].workClass = workClass;
237 result = pthread_create(ids +
i, NULL,
_threadMain, (
void*) (arguments +
i));
241 std::cerr <<
"ThreadManager: can't create threads." << std::endl;
255 for (
int i = 0;
i < threads; ++
i)
256 pthread_join(ids[
i], NULL);
266 runAsync(
function, data);
275 workFunction =
function;
291 if (!(nTasks && bSize && bSize <= nTasks))
294 numberOfTasks = nTasks;
321 bool result = distribute(first, last);
328 if (tasks < 0 || tasks >= numberOfTasks)
331 assignedTasks = tasks;
350 if (assignedTasks >= numberOfTasks)
356 first = assignedTasks;
358 = (assignedTasks + blockSize < numberOfTasks) ? (assignedTasks
359 + blockSize) : numberOfTasks;
360 last = assignedTasks - 1;
370 pthread_mutex_init(&barrier->
mutex, NULL);
371 pthread_cond_init(&barrier->
cond, NULL);
377 pthread_mutex_destroy(&barrier->
mutex);
378 pthread_cond_destroy(&barrier->
cond);
384 pthread_mutex_lock(&barrier->
mutex);
389 pthread_cond_broadcast(&barrier->
cond);
393 pthread_cond_wait(&barrier->
cond,&barrier->
mutex);
395 pthread_mutex_unlock(&barrier->
mutex);
void run(ThreadFunction function, void *data=NULL)
int barrier_init(barrier_t *barrier, int needed)
void * _threadMain(void *data)
virtual bool distribute(size_t &first, size_t &last)
bool setAssignedTasks(size_t tasks)
#define REPORT_ERROR(nerr, ErrormMsg)
bool getTasks(size_t &first, size_t &last)
int threads
number of working threads.
int called
How many threads already arrived.
void setData(void *data, int nThread=-1)
int barrier_destroy(barrier_t *barrier)
void setBlockSize(size_t bSize)
void * _singleThreadMain(void *data)
void(* ThreadFunction)(ThreadArgument &arg)
Incorrect argument received.
int needed
How many threads should be awaited.
pthread_cond_t cond
Condition on which the threads are waiting.
ThreadManager(int numberOfThreads, void *workClass=NULL)
pthread_mutex_t mutex
Mutex to update this structure.
ParallelTaskDistributor(size_t nTasks, size_t bSize)
int thread_id
The thread id.
Barrier(int numberOfThreads)
void runAsync(ThreadFunction function, void *data=NULL)
int barrier_wait(barrier_t *barrier)