votca
2024.2-dev
Loading...
Searching...
No Matches
tools
src
libtools
thread.cc
Go to the documentation of this file.
1
/*
2
* Copyright 2009-2020 The VOTCA Development Team (http://www.votca.org)
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*
16
*/
17
18
// Standard includes
19
#include <stdexcept>
20
21
// Local VOTCA includes
22
#include "
votca/tools/lexical_cast.h
"
23
#include "
votca/tools/thread.h
"
24
#include "
votca/tools/types.h
"
25
26
using namespace
std
;
27
28
namespace
votca
{
29
namespace
tools {
30
31
void
*
runwrapper
(
void
*arg) {
32
Thread
*thread = (
Thread
*)(arg);
33
thread->
Run
();
34
pthread_exit(
nullptr
);
35
return
nullptr
;
36
}
37
38
Thread::Thread
() =
default
;
39
40
Thread::~Thread
() =
default
;
41
42
void
Thread::Start
() {
43
44
pthread_attr_init(&
attr_
);
45
/*
46
* according to the POSIX standard, threads are created in the joinable state
47
* by default
48
* however, some platforms do not obey
49
*
50
* explicitly create the thread in the joinable state
51
* the main program can then wait for the thread by calling
52
* pthread_join(thread)
53
*
54
*/
55
pthread_attr_setdetachstate(&
attr_
, PTHREAD_CREATE_JOINABLE);
56
finished_
=
false
;
57
58
Index
rc =
59
pthread_create(&
thread_
, &
attr_
,
runwrapper
,
static_cast<
void
*
>
(
this
));
60
if
(rc) {
61
throw
std::runtime_error(
"ERROR; return code from pthread_create() is "
+
62
boost::lexical_cast<std::string>(rc));
63
}
64
}
65
66
void
Thread::WaitDone
() {
67
void
*status;
68
Index
rc = pthread_join(
thread_
, &status);
69
if
(rc) {
70
throw
std::runtime_error(
"ERROR; return code from pthread_join() is "
+
71
boost::lexical_cast<std::string>(rc));
72
}
73
finished_
=
true
;
74
pthread_attr_destroy(&
attr_
);
75
}
76
77
bool
Thread::IsFinished
()
const
{
return
finished_
; }
78
}
// namespace tools
79
}
// namespace votca
votca::tools::Thread
Framework for threaded execution.
Definition
thread.h:35
votca::tools::Thread::finished_
bool finished_
Definition
thread.h:73
votca::tools::Thread::thread_
pthread_t thread_
Definition
thread.h:72
votca::tools::Thread::runwrapper
friend void * runwrapper(void *arg)
Definition
thread.cc:31
votca::tools::Thread::Run
virtual void Run(void)=0
Run() executes the actual code.
votca::tools::Thread::attr_
pthread_attr_t attr_
Definition
thread.h:71
votca::tools::Thread::WaitDone
void WaitDone()
WaitDone() will not exit until thread ends computation.
Definition
thread.cc:66
votca::tools::Thread::Start
void Start()
Starts and runs a thread.
Definition
thread.cc:42
votca::tools::Thread::Thread
Thread()
votca::tools::Thread::~Thread
virtual ~Thread()
votca::tools::Thread::IsFinished
bool IsFinished() const
Checks to see if the thread is done.
Definition
thread.cc:77
lexical_cast.h
std
STL namespace.
votca::tools::runwrapper
void * runwrapper(void *arg)
Definition
thread.cc:31
votca
base class for all analysis tools
Definition
basebead.h:33
votca::Index
Eigen::Index Index
Definition
types.h:26
thread.h
types.h
Generated by
1.12.0