|
NCBI Home IEB Home C++ Toolkit docs C Toolkit source browser C Toolkit source browser (2) |
NCBI C++ Toolkit Cross ReferenceC++/src/util/thread_pool_old.cpp |
source navigation diff markup identifier search freetext search file search |
1 /* $Id: thread_pool_old.cpp 122643 2008-03-24 13:44:29Z ivanovp $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Author: Aaron Ucko
27 *
28 * File Description:
29 * Pools of generic request-handling threads.
30 */
31
32 #include <ncbi_pch.hpp>
33 #include <util/thread_pool.hpp>
34 #include <corelib/ncbi_system.hpp>
35 #include <algorithm>
36
37 BEGIN_NCBI_SCOPE
38
39
40 class CFatalRequest : public CStdRequest
41 {
42 protected:
43 void Process(void) { CThread::Exit(0); } // Kill the current thread
44 };
45
46
47 void CStdPoolOfThreads::KillAllThreads(bool wait)
48 {
49 TACValue n, old_max;
50 {{
51 CMutexGuard guard(m_Mutex);
52 old_max = m_MaxThreads;
53 m_MaxThreads = 0; // Forbid spawning new threads
54 n = m_ThreadCount.Get(); // Capture for use without mutex
55 }}
56
57 CRef<CStdRequest> poison(new CFatalRequest);
58
59 for (TACValue i = 0; i < n; ) {
60 try {
61 WaitForRoom();
62 AcceptRequest(poison);
63 ++i;
64 } catch (CBlockingQueueException&) { // guard against races
65 continue;
66 }
67 }
68 NON_CONST_ITERATE(TThreads, it, m_Threads) {
69 if (wait) {
70 (*it)->Join();
71 } else {
72 (*it)->Detach();
73 }
74 }
75 m_Threads.clear();
76 {{
77 CMutexGuard guard(m_Mutex);
78 m_MaxThreads = old_max;
79 }}
80 }
81
82
83 void CStdPoolOfThreads::Register(TThread& thread)
84 {
85 CMutexGuard guard(m_Mutex);
86 if (m_MaxThreads > 0) {
87 m_Threads.push_back(CRef<TThread>(&thread));
88 }
89 }
90
91 void CStdPoolOfThreads::UnRegister(TThread& thread)
92 {
93 CMutexGuard guard(m_Mutex);
94 if (m_MaxThreads > 0) {
95 TThreads::iterator it = find(m_Threads.begin(), m_Threads.end(),
96 CRef<TThread>(&thread));
97 if (it != m_Threads.end()) {
98 (*it)->Detach();
99 m_Threads.erase(it);
100 }
101 }
102 }
103
104 CStdPoolOfThreads::~CStdPoolOfThreads()
105 {
106 try {
107 KillAllThreads(false);
108 } catch(...) {} // Just to be sure that we will not throw from the destructor.
109 }
110
111 END_NCBI_SCOPE
112 |
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more information. |