NCBI C++ ToolKit
thread_nonstop.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: thread_nonstop.cpp 41082 2009-02-17 20:40:02Z grichenk $
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: Anatoliy Kuznetsov
27  *
28  * File Description: BDB libarary types implementations.
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 #include <util/thread_nonstop.hpp>
34 #include <corelib/ncbi_system.hpp>
35 
36 
38 
39 
40 CThreadNonStop::CThreadNonStop(unsigned run_delay,
41  unsigned)
42 : m_RunInterval(run_delay),
43  m_StopSignal(0, 10000000)
44 {
45 }
46 
48 {
49  return m_StopFlag.Get() != 0;
50 }
51 
53 {
54  m_StopFlag.Add(1);
56 }
57 
59 {
61 }
62 
64 {
65  while (1) {
66 
67  DoJob();
68 
69  bool flag = m_StopSignal.TryWait(m_RunInterval, 0);
70  if (flag) {
71  if (m_StopFlag.Get() != 0) {
72  break;
73  }
74  }
75 
76  } // for flag
77 
78  return 0;
79 }
80 
81 
void RequestStop()
Schedule thread Stop.
CSemaphore m_StopSignal
void RequestDoJob()
Interrupt the thread sleeping condition and force DoJob() run.
CThreadNonStop(unsigned run_delay, unsigned stop_request_poll=10)
Thread runs indefinetely with run_delay intervals the delay between job executions.
CAtomicCounter_WithAutoInit m_StopFlag
virtual void * Main(void)
Overload from CThread.
bool IsStopRequested() const
Return TRUE if thread stop has been requested.
virtual void DoJob(void)=0
Payload function.
unsigned int m_RunInterval
TValue Add(int delta) THROWS_NONE
Atomically add value (=delta), and return new counter value.
Definition: ncbicntr.hpp:278
TValue Get(void) const THROWS_NONE
Get atomic counter value.
Definition: ncbicntr.hpp:168
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
bool TryWait(unsigned int timeout_sec=0, unsigned int timeout_nsec=0)
Timed wait.
Definition: ncbimtx.cpp:1844
void Post(unsigned int count=1)
Increment the semaphore by "count".
Definition: ncbimtx.cpp:1971
Modified on Fri Sep 20 14:58:14 2024 by modify_doxy.py rev. 669887