ATLAS Offline Software
Public Member Functions | Private Attributes | List of all members
JiveXML::ThreadCollection Class Reference

This class handles a collection of threads. More...

#include <ONCRPCThreadCollection.h>

Inheritance diagram for JiveXML::ThreadCollection:
Collaboration diagram for JiveXML::ThreadCollection:

Public Member Functions

 ThreadCollection ()
 
 ~ThreadCollection ()
 

Private Attributes

elements
 STL member. More...
 

Public interface functions

pthread_mutex_t m_mutex
 
sem_t m_semaphore
 
void AddThread (const pthread_t &thread)
 
void WaitAdd ()
 
void RemoveThread (const pthread_t &thread)
 
void JoinAll ()
 
int NumberOfThreads ()
 

Detailed Description

This class handles a collection of threads.

Definition at line 17 of file ONCRPCThreadCollection.h.

Constructor & Destructor Documentation

◆ ThreadCollection()

JiveXML::ThreadCollection::ThreadCollection ( )

Definition at line 11 of file ONCRPCThreadCollection.cxx.

11  {
12 
13  //intialize the mutex with default attributes
14  pthread_mutex_init(&m_mutex, NULL);
15 
16  //initialize the semaphore
17  sem_init(&m_semaphore,0,0);
18 
19  }

◆ ~ThreadCollection()

JiveXML::ThreadCollection::~ThreadCollection ( )

Definition at line 22 of file ONCRPCThreadCollection.cxx.

22  {
23 
24  //destroy the mutex
25  pthread_mutex_destroy(&m_mutex);
26 
27  //and the semaphore
28  sem_destroy(&m_semaphore);
29 
30  }

Member Function Documentation

◆ AddThread()

void JiveXML::ThreadCollection::AddThread ( const pthread_t &  thread)

Definition at line 34 of file ONCRPCThreadCollection.cxx.

34  {
35 
36  //First get a mutex //RETVAL for all of them!
37  pthread_mutex_lock(&m_mutex);
38 
39  //Now add the thread to the list of vectors
40  push_back(thread);
41 
42  //Then remove mutex again
43  pthread_mutex_unlock(&m_mutex);
44 
45  //And signal any potentially waiting threads
46  sem_post(&m_semaphore);
47 
48  }

◆ JoinAll()

void JiveXML::ThreadCollection::JoinAll ( )

Definition at line 88 of file ONCRPCThreadCollection.cxx.

88  {
89 
90  //The threads are removing themselves from the list,
91  //so iterators are getting invalid while we are looping.
92  //However, some threads may have crashed w/o being able to remove themselves
93  //So wait for all of them to finish w/o keeping the mutex locked while
94  //waiting
95 
96  //Loop till all threads are gone
97  while ( size() > 0 ){
98 
99  //First get a mutex //RETVAL for all of them!
100  pthread_mutex_lock(&m_mutex);
101 
102  //Order is not important - take the first element
103  pthread_t thread = *begin();
104 
105  //Then remove mutex again
106  pthread_mutex_unlock(&m_mutex);
107 
108  //Wait for that thread to finish
109  pthread_join(thread,NULL);
110 
111  //Now remove it - if it has already removed itself, nothing will happen
112  //If it hadn't removed itself, we will remove it
113  RemoveThread(thread);
114  }
115  }

◆ NumberOfThreads()

int JiveXML::ThreadCollection::NumberOfThreads ( )

Definition at line 119 of file ONCRPCThreadCollection.cxx.

119  {
120 
121  //First get a mutex
122  pthread_mutex_lock(&m_mutex);
123 
124  //Get number of elements
125  int NThreads = size();
126 
127  //Then remove mutex again
128  pthread_mutex_unlock(&m_mutex);
129 
130  //finally return size
131  return NThreads;
132  }

◆ RemoveThread()

void JiveXML::ThreadCollection::RemoveThread ( const pthread_t &  thread)

Definition at line 58 of file ONCRPCThreadCollection.cxx.

58  {
59 
60  //First get a mutex
61  pthread_mutex_lock(&m_mutex);
62 
63  //Loop over list and find that entry
64  ThreadCollection::iterator threadItr = begin();
65  while ( threadItr != end() ){
66  //See if this is the thread we are looking for
67  if ( *threadItr == thread ){
68  //remove it from the collection
69  erase(threadItr);
70  //iterator is invalid, we removed thread, so stop looping
71  break ;
72  }
73  //Go to next thread
74  ++threadItr;
75  }
76 
77  //Then remove mutex again
78  pthread_mutex_unlock(&m_mutex);
79 
80  //Set this threads state to detached, so its
81  //resources are reclaimed once it
82  //finishes.
83  pthread_detach(thread);
84 
85  }

◆ WaitAdd()

void JiveXML::ThreadCollection::WaitAdd ( )

Definition at line 51 of file ONCRPCThreadCollection.cxx.

51  {
52 
53  //simply wait for the access semaphore to be set
54  sem_wait(&m_semaphore);
55  }

Member Data Documentation

◆ elements

T std::vector< T >::elements
inherited

STL member.

◆ m_mutex

pthread_mutex_t JiveXML::ThreadCollection::m_mutex
private

Definition at line 48 of file ONCRPCThreadCollection.h.

◆ m_semaphore

sem_t JiveXML::ThreadCollection::m_semaphore
private

Definition at line 50 of file ONCRPCThreadCollection.h.


The documentation for this class was generated from the following files:
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
JiveXML::ThreadCollection::m_mutex
pthread_mutex_t m_mutex
Definition: ONCRPCThreadCollection.h:48
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
JiveXML::ThreadCollection::m_semaphore
sem_t m_semaphore
Definition: ONCRPCThreadCollection.h:50
JiveXML::ThreadCollection::RemoveThread
void RemoveThread(const pthread_t &thread)
Definition: ONCRPCThreadCollection.cxx:58