Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
wolf
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mobile_robotics
wolf_projects
wolf_lib
wolf
Commits
a74b17ac
Commit
a74b17ac
authored
8 years ago
by
Jeremie Deray
Browse files
Options
Downloads
Patches
Plain Diff
add ThreadedBaseClass
parent
7be3f4ec
No related branches found
No related tags found
1 merge request
!90
[WIP] ProcessorBase multi-threading
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/threaded_base_class.cpp
+66
-0
66 additions, 0 deletions
src/threaded_base_class.cpp
src/threaded_base_class.h
+89
-0
89 additions, 0 deletions
src/threaded_base_class.h
with
155 additions
and
0 deletions
src/threaded_base_class.cpp
0 → 100644
+
66
−
0
View file @
a74b17ac
#include
"threaded_base_class.h"
namespace
wolf
{
namespace
core
{
ThreadedBaseClass
::
ThreadedBaseClass
(
unsigned
int
max_frequency
)
:
run_
(
false
),
period_
(
1000
/
max_frequency
)
{
//
}
ThreadedBaseClass
::~
ThreadedBaseClass
()
{
if
(
run_
.
load
(
std
::
memory_order_acquire
)
)
stop
();
}
void
ThreadedBaseClass
::
run
()
{
if
(
run_
.
load
(
std
::
memory_order_acquire
)
)
return
;
run_
.
store
(
true
,
std
::
memory_order_release
);
thread_
=
std
::
thread
(
&
ThreadedBaseClass
::
execute
,
this
);
//_future = std::async(std::launch::async, &Talker::execute, this);
}
void
ThreadedBaseClass
::
stop
()
{
run_
.
store
(
false
,
std
::
memory_order_release
);
cv_
.
notify_one
();
if
(
thread_
.
joinable
()
)
thread_
.
join
();
}
bool
ThreadedBaseClass
::
isRunning
()
{
return
run_
.
load
(
std
::
memory_order_acquire
);
}
void
ThreadedBaseClass
::
execute
()
{
while
(
run_
.
load
(
std
::
memory_order_acquire
)
)
{
auto
start
=
std
::
chrono
::
system_clock
::
now
();
executeImpl
();
auto
end
=
std
::
chrono
::
system_clock
::
now
();
auto
elapsed
=
std
::
chrono
::
duration_cast
<
ms
>
(
end
-
start
);
auto
diff
=
period_
-
elapsed
;
// @Todo warn if diff < 0.
// meaning that the process is actually slower
// than the user defined period (max_frequency).
diff
=
(
diff
>=
ms
(
0
))
?
diff
:
ms
(
0
);
sleep
(
diff
);
}
}
}
// namespace core
}
// namespace wolf
This diff is collapsed.
Click to expand it.
src/threaded_base_class.h
0 → 100644
+
89
−
0
View file @
a74b17ac
/**
* \file threaded_class.h
* \brief A base class for self-threaded object
* \author Jeremie Deray
* Created on: 26/09/2016
*/
#ifndef WOLF_THREADED_CLASS_BASE_H_
#define WOLF_THREADED_CLASS_BASE_H_
#include
<thread>
#include
<mutex>
#include
<iostream>
#include
<future>
namespace
wolf
{
namespace
core
{
template
<
typename
_Rep
,
typename
_Period
>
void
sleep
(
const
std
::
chrono
::
duration
<
_Rep
,
_Period
>&
dur
)
{
std
::
this_thread
::
sleep_for
(
std
::
forward
<
const
std
::
chrono
::
duration
<
_Rep
,
_Period
>
>
(
dur
));
}
class
ThreadedBaseClass
{
using
ms
=
std
::
chrono
::
milliseconds
;
public:
/**
* \brief ThreadedClass Base class that wraps an 'execute' function in a thread.
*
* \param name An identification name.
*
* \param max_frequency The maximum frequency at which the thread should run in Hz.
* It might actually be slower depending on the load of 'executeImpl'.
*
* \see executeImpl
**/
ThreadedBaseClass
(
unsigned
int
max_frequency
);
virtual
~
ThreadedBaseClass
();
/**
* \brief Starts Main loop.
* Creates thread in which 'execute' is called.
**/
virtual
void
run
();
/**
* \brief Main loop.
**/
virtual
void
stop
();
bool
isRunning
();
protected:
std
::
atomic
<
bool
>
run_
;
std
::
condition_variable
cv_
;
/** \brief Max period of the execution loop in nanosec */
ms
period_
;
std
::
thread
thread_
;
//std::future<DataPtr> _future;
/**
* \brief execute. Main loop.
**/
virtual
void
execute
();
/**
* \brief Derived has to provide this function.
**/
virtual
void
executeImpl
()
=
0
;
};
}
// namespace core
}
// namespace wolf
#endif
/* WOLF_THREADED_CLASS_BASE_H_ */
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment