ATLAS Offline Software
Loading...
Searching...
No Matches
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.

Public interface functions

std::mutex 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 18 of file ONCRPCThreadCollection.h.

Constructor & Destructor Documentation

◆ ThreadCollection()

JiveXML::ThreadCollection::ThreadCollection ( )

Definition at line 11 of file ONCRPCThreadCollection.cxx.

11 {
12
13 //initialize the semaphore
14 sem_init(&m_semaphore,0,0);
15
16 }

◆ ~ThreadCollection()

JiveXML::ThreadCollection::~ThreadCollection ( )

Definition at line 19 of file ONCRPCThreadCollection.cxx.

19 {
20
21 //Destroy the semaphore
22 sem_destroy(&m_semaphore);
23
24 }

Member Function Documentation

◆ AddThread()

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

Definition at line 28 of file ONCRPCThreadCollection.cxx.

28 {
29
30 //Now add the thread to the list of vectors
31 {
32 std::lock_guard lock (m_mutex);
33 push_back(thread);
34 }
35
36 //And signal any potentially waiting threads
37 sem_post(&m_semaphore);
38
39 }

◆ JoinAll()

void JiveXML::ThreadCollection::JoinAll ( )

Definition at line 76 of file ONCRPCThreadCollection.cxx.

76 {
77
78 //The threads are removing themselves from the list,
79 //so iterators are getting invalid while we are looping.
80 //However, some threads may have crashed w/o being able to remove themselves
81 //So wait for all of them to finish w/o keeping the mutex locked while
82 //waiting
83
84 //Loop till all threads are gone
85 while ( size() > 0 ){
86
87 //Order is not important - take the first element
88 pthread_t thread;
89 {
90 std::lock_guard lock (m_mutex);
91 thread = *begin();
92 }
93
94 //Wait for that thread to finish
95 pthread_join(thread,NULL);
96
97 //Now remove it - if it has already removed itself, nothing will happen
98 //If it hadn't removed itself, we will remove it
99 RemoveThread(thread);
100 }
101 }
void RemoveThread(const pthread_t &thread)

◆ NumberOfThreads()

int JiveXML::ThreadCollection::NumberOfThreads ( )

Definition at line 105 of file ONCRPCThreadCollection.cxx.

105 {
106
107 std::lock_guard lock (m_mutex);
108 //Return number of elements
109 return size();
110 }

◆ RemoveThread()

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

Definition at line 49 of file ONCRPCThreadCollection.cxx.

49 {
50
51 //First get a mutex
52 std::lock_guard lock (m_mutex);
53
54 //Loop over list and find that entry
55 ThreadCollection::iterator threadItr = begin();
56 while ( threadItr != end() ){
57 //See if this is the thread we are looking for
58 if ( *threadItr == thread ){
59 //remove it from the collection
60 erase(threadItr);
61 //iterator is invalid, we removed thread, so stop looping
62 break ;
63 }
64 //Go to next thread
65 ++threadItr;
66 }
67
68 //Set this threads state to detached, so its
69 //resources are reclaimed once it
70 //finishes.
71 pthread_detach(thread);
72
73 }

◆ WaitAdd()

void JiveXML::ThreadCollection::WaitAdd ( )

Definition at line 42 of file ONCRPCThreadCollection.cxx.

42 {
43
44 //simply wait for the access semaphore to be set
45 sem_wait(&m_semaphore);
46 }

Member Data Documentation

◆ elements

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

STL member.

◆ m_mutex

std::mutex JiveXML::ThreadCollection::m_mutex
private

Definition at line 49 of file ONCRPCThreadCollection.h.

◆ m_semaphore

sem_t JiveXML::ThreadCollection::m_semaphore
private

Definition at line 51 of file ONCRPCThreadCollection.h.


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