Nektar++
Public Member Functions | Private Member Functions | Private Attributes | List of all members
Nektar::Thread::ThreadWorkerStd Class Reference

Implementation class for ThreadManagerStd. More...

#include <ThreadStd.h>

Public Member Functions

 ThreadWorkerStd (ThreadManagerStd *threadManager, unsigned int workerNum)
 Constructor. More...
 
 ~ThreadWorkerStd ()
 Destructor. More...
 
void operator() ()
 This provides the interface that std::thread uses to start the worker. More...
 
unsigned int GetWorkerNum ()
 Return the index of the worker thread. More...
 
void Stop ()
 A signal to shut down. More...
 

Private Member Functions

 ThreadWorkerStd ()
 
 ThreadWorkerStd (const ThreadWorkerStd &)
 
void MainLoop ()
 
void LoadJobs ()
 
unsigned int GetNumToLoad ()
 
void WaitForActive ()
 
void RunJobs ()
 

Private Attributes

ThreadManagerStdm_threadManager
 
std::queue< ThreadJob * > m_workerQueue
 
bool m_keepgoing
 
unsigned int m_threadNum
 

Detailed Description

Implementation class for ThreadManagerStd.

Each instance of this class corresponds to a worker thread. Instances manage their own queue of jobs to run, grabbing new jobs from the master queue when it is exhausted.

Definition at line 158 of file ThreadStd.h.

Constructor & Destructor Documentation

◆ ThreadWorkerStd() [1/3]

Nektar::Thread::ThreadWorkerStd::ThreadWorkerStd ( ThreadManagerStd tm,
unsigned int  workerNum 
)

Constructor.

Parameters
threadManagerPointer to the ThreadManagerStd that is controlling this worker.
workerNumUnique number from 0..(number_of_threads - 1)

Called by the ThreadManagerStd instance.

Definition at line 312 of file ThreadStd.cpp.

314 m_threadNum(workerNum)
315{
316 // Nothing to see here
317}
std::queue< ThreadJob * > m_workerQueue
Definition: ThreadStd.h:201
ThreadManagerStd * m_threadManager
Definition: ThreadStd.h:200

◆ ~ThreadWorkerStd()

Nektar::Thread::ThreadWorkerStd::~ThreadWorkerStd ( )

Destructor.

Winds up this thread's execution. Jobs in its queue are lost.

Definition at line 322 of file ThreadStd.cpp.

323{
324 if (m_keepgoing)
325 {
326 std::cerr << "Warning: ThreadWorker: " << m_threadNum
327 << "destroyed while running!" << std::endl;
328 }
329 // on destuction the m_workerQueue will be destructed and that
330 // will destruct any ThreadJobs still in there.
331}

References m_keepgoing, and m_threadNum.

◆ ThreadWorkerStd() [2/3]

Nektar::Thread::ThreadWorkerStd::ThreadWorkerStd ( )
private

◆ ThreadWorkerStd() [3/3]

Nektar::Thread::ThreadWorkerStd::ThreadWorkerStd ( const ThreadWorkerStd )
private

Member Function Documentation

◆ GetNumToLoad()

unsigned int Nektar::Thread::ThreadWorkerStd::GetNumToLoad ( )
private

Definition at line 370 of file ThreadStd.cpp.

371{
372 unsigned int numToLoad = 0;
374 {
375 case e_guided:
376 numToLoad = std::max(
377 static_cast<unsigned long>(m_threadManager->m_chunkSize),
378 static_cast<unsigned long>(
380 (2 * m_threadManager->m_numWorkers + 1)));
381 break;
382
383 case e_dynamic:
384 numToLoad = m_threadManager->m_chunkSize;
385 break;
386
387 default:
388 NEKERROR(ErrorUtil::efatal, "Invalid value for SchedType.");
389 break;
390 }
391 return numToLoad;
392}
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:202
std::queue< ThreadJob * > m_masterQueue
Definition: ThreadStd.h:133

References Nektar::Thread::e_dynamic, Nektar::Thread::e_guided, Nektar::ErrorUtil::efatal, Nektar::Thread::ThreadManagerStd::m_chunkSize, Nektar::Thread::ThreadManagerStd::m_masterQueue, Nektar::Thread::ThreadManagerStd::m_numWorkers, Nektar::Thread::ThreadManagerStd::m_schedType, m_threadManager, and NEKERROR.

Referenced by LoadJobs().

◆ GetWorkerNum()

unsigned int Nektar::Thread::ThreadWorkerStd::GetWorkerNum ( )
inline

Return the index of the worker thread.

Returns
Index of worker thread, an integer between 0 and (number_of_threads - 1)

Definition at line 175 of file ThreadStd.h.

176 {
177 return m_threadNum;
178 };

References m_threadNum.

◆ LoadJobs()

void Nektar::Thread::ThreadWorkerStd::LoadJobs ( )
private

Definition at line 336 of file ThreadStd.cpp.

337{
338 // Lock the master queue
339 Lock masterQueueLock(m_threadManager->m_masterQueueMutex);
342 while (m_threadManager->m_masterQueue.empty() && m_keepgoing)
343 {
344 // while waiting, master queue is unlocked
345 m_threadManager->m_masterQueueCondVar.wait(masterQueueLock);
346 // on exiting wait master queue is locked again
347 }
348 bool active;
349 {
350 Lock masterActiveLock(m_threadManager->m_masterActiveMutex);
352 }
353 if (active && m_keepgoing)
354 {
355 unsigned int numToLoad = GetNumToLoad();
356 while (m_workerQueue.size() < numToLoad &&
358 {
359 ThreadJob *tj = m_threadManager->m_masterQueue.front();
360 m_workerQueue.push(tj);
362 }
363 }
365} // lock on master queue released here
std::condition_variable m_masterQueueCondVar
Definition: ThreadStd.h:136
std::unique_lock< std::mutex > Lock
Definition: ThreadStd.h:50

References GetNumToLoad(), m_keepgoing, Nektar::Thread::ThreadManagerStd::m_masterActiveMutex, Nektar::Thread::ThreadManagerStd::m_masterQueue, Nektar::Thread::ThreadManagerStd::m_masterQueueCondVar, Nektar::Thread::ThreadManagerStd::m_masterQueueMutex, Nektar::Thread::ThreadManagerStd::m_threadActiveList, Nektar::Thread::ThreadManagerStd::m_threadBusyList, m_threadManager, m_threadNum, and m_workerQueue.

Referenced by MainLoop().

◆ MainLoop()

void Nektar::Thread::ThreadWorkerStd::MainLoop ( )
private

Definition at line 412 of file ThreadStd.cpp.

413{
414 while (m_keepgoing)
415 {
417 LoadJobs();
418 RunJobs();
419 }
420} // exiting here should terminate the thread

References LoadJobs(), m_keepgoing, RunJobs(), and WaitForActive().

Referenced by operator()().

◆ operator()()

void Nektar::Thread::ThreadWorkerStd::operator() ( )
inline

This provides the interface that std::thread uses to start the worker.

Definition at line 166 of file ThreadStd.h.

167 {
168 MainLoop();
169 };

References MainLoop().

◆ RunJobs()

void Nektar::Thread::ThreadWorkerStd::RunJobs ( )
private

Definition at line 425 of file ThreadStd.cpp.

426{
427 while (!m_workerQueue.empty() && m_keepgoing)
428 {
429 ThreadJob *tj;
430 try
431 {
432 tj = m_workerQueue.front();
433 tj->SetWorkerNum(m_threadNum);
434 tj->Run();
435 m_workerQueue.pop();
436 delete tj;
437 }
438 catch (...)
439 {
440 // something bad happened, probably time to die
441 // maybe signal ThreadManager
442 throw;
443 }
444 }
445}

References m_keepgoing, m_threadNum, m_workerQueue, Nektar::Thread::ThreadJob::Run(), and Nektar::Thread::ThreadJob::SetWorkerNum().

Referenced by MainLoop().

◆ Stop()

void Nektar::Thread::ThreadWorkerStd::Stop ( )
inline

A signal to shut down.

If this method is called the worker will shut down. Used by the ThreadManagerStd to stop threading.

Definition at line 185 of file ThreadStd.h.

186 {
187 m_keepgoing = false;
188 };

References m_keepgoing.

Referenced by Nektar::Thread::ThreadManagerStd::~ThreadManagerStd().

◆ WaitForActive()

void Nektar::Thread::ThreadWorkerStd::WaitForActive ( )
private

Definition at line 397 of file ThreadStd.cpp.

398{
399 Lock masterActiveLock(m_threadManager->m_masterActiveMutex);
400
402 {
403 // while waiting, master active is unlocked
404 m_threadManager->m_masterActiveCondVar.wait(masterActiveLock);
405 // on exiting wait master active is locked again
406 }
407}
std::condition_variable m_masterActiveCondVar
Definition: ThreadStd.h:137

References m_keepgoing, Nektar::Thread::ThreadManagerStd::m_masterActiveCondVar, Nektar::Thread::ThreadManagerStd::m_masterActiveMutex, Nektar::Thread::ThreadManagerStd::m_threadActiveList, m_threadManager, and m_threadNum.

Referenced by MainLoop().

Member Data Documentation

◆ m_keepgoing

bool Nektar::Thread::ThreadWorkerStd::m_keepgoing
private

Definition at line 202 of file ThreadStd.h.

Referenced by LoadJobs(), MainLoop(), RunJobs(), Stop(), WaitForActive(), and ~ThreadWorkerStd().

◆ m_threadManager

ThreadManagerStd* Nektar::Thread::ThreadWorkerStd::m_threadManager
private

Definition at line 200 of file ThreadStd.h.

Referenced by GetNumToLoad(), LoadJobs(), and WaitForActive().

◆ m_threadNum

unsigned int Nektar::Thread::ThreadWorkerStd::m_threadNum
private

Definition at line 203 of file ThreadStd.h.

Referenced by GetWorkerNum(), LoadJobs(), RunJobs(), WaitForActive(), and ~ThreadWorkerStd().

◆ m_workerQueue

std::queue<ThreadJob *> Nektar::Thread::ThreadWorkerStd::m_workerQueue
private

Definition at line 201 of file ThreadStd.h.

Referenced by LoadJobs(), and RunJobs().