ATLAS Offline Software
Loading...
Searching...
No Matches
ONCRPCThreadCollection.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5
7
8namespace JiveXML{
9
10 //Constructor
12
13 //initialize the semaphore
14 sem_init(&m_semaphore,0,0);
15
16 }
17
18 //Constructor
20
21 //Destroy the semaphore
22 sem_destroy(&m_semaphore);
23
24 }
25
26 //Add a thread to the container - don't need to be too strict
27 //If a thread is double added, joinAll will still remove it.
28 void ThreadCollection::AddThread( const pthread_t& thread ){
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 }
40
41 //Wait until a thread has been added
43
44 //simply wait for the access semaphore to be set
45 sem_wait(&m_semaphore);
46 }
47
48 //Remove a thread
49 void ThreadCollection::RemoveThread( const pthread_t& thread ){
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 }
74
75 //Wait for all threads to finish
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 }
102
103
104 //Return number of elements in the vector
106
107 std::lock_guard lock (m_mutex);
108 //Return number of elements
109 return size();
110 }
111
112}//namespace
void RemoveThread(const pthread_t &thread)
void AddThread(const pthread_t &thread)
This header is shared inbetween the C-style server thread and the C++ Athena ServerSvc.