votca 2024-dev
Loading...
Searching...
No Matches
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
23#include "votca/tools/thread.h"
24#include "votca/tools/types.h"
25
26using namespace std;
27
28namespace votca {
29namespace tools {
30
31void *runwrapper(void *arg) {
32 Thread *thread = (Thread *)(arg);
33 thread->Run();
34 pthread_exit(nullptr);
35 return nullptr;
36}
37
38Thread::Thread() = default;
39
40Thread::~Thread() = default;
41
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
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
77bool Thread::IsFinished() const { return finished_; }
78} // namespace tools
79} // namespace votca
Framework for threaded execution.
Definition thread.h:35
pthread_t thread_
Definition thread.h:72
friend void * runwrapper(void *arg)
Definition thread.cc:31
virtual void Run(void)=0
Run() executes the actual code.
pthread_attr_t attr_
Definition thread.h:71
void WaitDone()
WaitDone() will not exit until thread ends computation.
Definition thread.cc:66
void Start()
Starts and runs a thread.
Definition thread.cc:42
bool IsFinished() const
Checks to see if the thread is done.
Definition thread.cc:77
STL namespace.
void * runwrapper(void *arg)
Definition thread.cc:31
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26