Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Scientific Software
Takin
tlibs
Commits
44d712f3
Commit
44d712f3
authored
Apr 07, 2020
by
Tobias WEBER
Browse files
updated thread pool
parent
9bfdb5cc
Changes
4
Hide whitespace changes
Inline
Side-by-side
LITERATURE
View file @
44d712f3
...
...
@@ -24,6 +24,7 @@ General Mathematics
* (Arfken 2013), G. B. Arfken, "Mathematical Methods for Physicists", 2013, ISBN: 978-0-12-384654-9.
* (Arens 2015), T. Arens et al., "Mathematik", 2015, ISBN: 978-3-642-44919-2
-------------------------------------------------------------------------------
General Physics
-------------------------------------------------------------------------------
...
...
helper/thread.h
View file @
44d712f3
...
...
@@ -16,15 +16,17 @@
#include <algorithm>
#include <type_traits>
#include <memory>
#include <boost/asio.hpp>
namespace
tl
{
/**
*
minimalistic
thread pool
*
wrapper for boost
thread pool
*/
template
<
class
t_func
>
class
ThreadPool
template
<
class
t_func
>
class
ThreadPool
{
public:
using
t_ret
=
typename
std
::
result_of
<
t_func
&
()
>::
type
;
...
...
@@ -33,65 +35,28 @@ template<class t_func> class ThreadPool
protected:
std
::
list
<
std
::
unique_ptr
<
std
::
thread
>>
m_lstThreads
;
mutable
std
::
mutex
m_mtx
;
boost
::
asio
::
thread_pool
m_tp
;
std
::
mutex
m_mtx
;
// list of wrapped function to be executed
t_task
m_lstTasks
;
// futures with function return values
t_fut
m_lstFutures
;
// signal to start jobs
std
::
promise
<
void
>
m_signalStartIn
;
std
::
future
<
void
>
m_signalStartOut
=
std
::
move
(
m_signalStartIn
.
get_future
());
void
(
*
m_pThStartFunc
)()
=
nullptr
;
public:
ThreadPool
(
unsigned
int
iNumThreads
=
std
::
thread
::
hardware_concurrency
(),
void
(
*
pThStartFunc
)()
=
nullptr
)
{
// start 'iNumThreads' threads
for
(
unsigned
int
iThread
=
0
;
iThread
<
iNumThreads
;
++
iThread
)
{
m_lstThreads
.
emplace_back
(
std
::
unique_ptr
<
std
::
thread
>
(
new
std
::
thread
([
this
,
pThStartFunc
,
iThread
]()
{
// callback to invoke before starting job thread
if
(
pThStartFunc
)
(
*
pThStartFunc
)();
m_signalStartOut
.
wait
();
while
(
1
)
{
std
::
unique_lock
<
std
::
mutex
>
lock0
(
m_mtx
);
// is a task available
if
(
m_lstTasks
.
size
()
>
0
)
{
// pop task from list
std
::
packaged_task
<
t_ret
()
>
task
=
std
::
move
(
m_lstTasks
.
front
());
m_lstTasks
.
pop_front
();
lock0
.
unlock
();
// run task
//tl::log_debug("Thread ", iThread, ": running task.");
task
();
}
else
break
;
}
})));
}
}
:
m_tp
{
iNumThreads
},
m_pThStartFunc
{
pThStartFunc
}
{}
virtual
~
ThreadPool
()
{
JoinAll
();
m_lstThreads
.
clear
();
Join
();
}
...
...
@@ -106,35 +71,33 @@ template<class t_func> class ThreadPool
std
::
lock_guard
<
std
::
mutex
>
lock
(
m_mtx
);
m_lstTasks
.
emplace_back
(
std
::
move
(
task
));
m_lstFutures
.
emplace_back
(
std
::
move
(
fut
));
}
std
::
packaged_task
<
t_ret
()
>*
thetask
=
&
m_lstTasks
.
back
();;
/**
* issue start signal
*/
void
StartTasks
()
{
m_signalStartIn
.
set_value
(
);
boost
::
asio
::
post
(
m_tp
,
[
this
,
thetask
]()
->
void
{
if
(
m_pThStartFunc
)
(
*
m_pThStartFunc
)
()
;
(
*
thetask
)();
}
);
}
t_fut
&
GetFutures
()
{
return
m_lstFutures
;
}
t_fut
&
GetResults
()
{
return
m_lstFutures
;
}
t_task
&
GetTasks
()
{
return
m_lstTasks
;
}
/**
* wait for all tasks to be finished
*/
void
Join
All
()
void
Join
()
{
std
::
for_each
(
m_lstThreads
.
begin
(),
m_lstThreads
.
end
(),
[](
std
::
unique_ptr
<
std
::
thread
>&
pThread
)
{
if
(
pThread
)
pThread
->
join
();
});
m_tp
.
join
();
}
};
}
#endif
log/log.cpp
View file @
44d712f3
...
...
@@ -184,6 +184,7 @@ Log::Log(const std::string& strInfo, LogColor col, std::ostream* pOstr)
Log
::~
Log
()
{
std
::
lock_guard
<
decltype
(
s_mtx
)
>
_lck
(
s_mtx
);
//std::cerr << "Removing " << m_strInfo << " logger." << std::endl;
m_mapOstrsTh
.
clear
();
m_vecOstrs
.
clear
();
...
...
phys/bz.h
View file @
44d712f3
...
...
@@ -181,9 +181,8 @@ class Brillouin3D
return
std
::
make_pair
(
true
,
planeperp
);
});
}
poolMiddlePerps
.
StartTasks
();
for
(
auto
&
fut
:
poolMiddlePerps
.
Get
Future
s
())
for
(
auto
&
fut
:
poolMiddlePerps
.
Get
Result
s
())
{
auto
pair
=
fut
.
get
();
if
(
pair
.
first
)
...
...
@@ -219,11 +218,10 @@ class Brillouin3D
}
}
}
poolIntersections
.
StartTasks
();
std
::
size_t
iVertsRemoved
=
0
;
Rt
<
T
,
3
,
8
,
0
>
rt
;
for
(
auto
&
fut
:
poolIntersections
.
Get
Future
s
())
for
(
auto
&
fut
:
poolIntersections
.
Get
Result
s
())
{
auto
pair
=
fut
.
get
();
if
(
pair
.
first
)
...
...
@@ -274,12 +272,11 @@ class Brillouin3D
return
std
::
make_pair
(
!
bRemoveVertex
,
iVert
);
});
}
poolVerts
.
StartTasks
();
std
::
vector
<
t_vec
<
T
>>
vecNewVertices
;
vecNewVertices
.
reserve
(
m_vecVertices
.
size
());
for
(
auto
&
fut
:
poolVerts
.
Get
Future
s
())
for
(
auto
&
fut
:
poolVerts
.
Get
Result
s
())
{
auto
pair
=
fut
.
get
();
if
(
pair
.
first
)
...
...
@@ -315,9 +312,8 @@ class Brillouin3D
return
std
::
make_tuple
(
bOk
,
vecPoly
,
plane
);
});
}
poolPolys
.
StartTasks
();
for
(
auto
&
fut
:
poolPolys
.
Get
Future
s
())
for
(
auto
&
fut
:
poolPolys
.
Get
Result
s
())
{
auto
tup
=
fut
.
get
();
if
(
std
::
get
<
0
>
(
tup
))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment