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
33992f57
Verified
Commit
33992f57
authored
Sep 11, 2020
by
Tobias WEBER
Browse files
threadpool: included changes from
https://github.com/t-weber/tlibs/blob/master/helper/thread.h
parent
ec96c62b
Changes
2
Hide whitespace changes
Inline
Side-by-side
helper/thread.h
View file @
33992f57
...
...
@@ -8,7 +8,12 @@
#ifndef __TLIBS_THREAD_H__
#define __TLIBS_THREAD_H__
#include <boost/asio.hpp>
//#define USE_OWN_THREADPOOL
#ifndef USE_OWN_THREADPOOL
#include <boost/asio.hpp>
#endif
#include <future>
#include <thread>
#include <mutex>
...
...
@@ -23,7 +28,7 @@ namespace tl {
/**
*
wrapper for boost
thread pool
* thread pool
*/
template
<
class
t_func
>
class
ThreadPool
...
...
@@ -35,7 +40,17 @@ class ThreadPool
protected:
#ifdef USE_OWN_THREADPOOL
std
::
list
<
std
::
unique_ptr
<
std
::
thread
>>
m_lstThreads
;
unsigned
int
m_tp
;
// dummy variable to avoid some ifdefs
// signal to start jobs
std
::
promise
<
void
>
m_signalStartIn
;
std
::
future
<
void
>
m_signalStartOut
=
std
::
move
(
m_signalStartIn
.
get_future
());
#else
boost
::
asio
::
thread_pool
m_tp
;
#endif
std
::
mutex
m_mtx
,
m_mtxStart
;
// list of wrapped function to be executed
...
...
@@ -52,7 +67,43 @@ class ThreadPool
ThreadPool
(
unsigned
int
iNumThreads
=
std
::
thread
::
hardware_concurrency
(),
void
(
*
pThStartFunc
)()
=
nullptr
)
:
m_tp
{
iNumThreads
},
m_pThStartFunc
{
pThStartFunc
}
{}
{
#ifdef USE_OWN_THREADPOOL
// 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 start function and task
CallStartFunc
();
task
();
}
else
break
;
}
})));
}
#endif
}
virtual
~
ThreadPool
()
...
...
@@ -73,31 +124,27 @@ class ThreadPool
m_lstTasks
.
emplace_back
(
std
::
move
(
task
));
m_lstFutures
.
emplace_back
(
std
::
move
(
fut
));
std
::
packaged_task
<
t_ret
()
>*
thetask
=
&
m_lstTasks
.
back
();;
#ifndef USE_OWN_THREADPOOL
std
::
packaged_task
<
t_ret
()
>*
thetask
=
&
m_lstTasks
.
back
();
boost
::
asio
::
post
(
m_tp
,
[
this
,
thetask
]()
->
void
{
{
// ensure that this is only called per-thread, not per-task
std
::
lock_guard
<
std
::
mutex
>
lockStart
(
m_mtxStart
);
thread_local
bool
bThreadAlreadySeen
{
0
};
if
(
m_pThStartFunc
&&
!
bThreadAlreadySeen
)
{
bThreadAlreadySeen
=
1
;
(
*
m_pThStartFunc
)();
}
}
CallStartFunc
();
(
*
thetask
)();
});
#endif
}
t_fut
&
GetResults
()
{
return
m_lstFutures
;
}
t_task
&
GetTasks
()
{
return
m_lstTasks
;
}
/**
* start tasks (does nothing when using boost threadpool)
*/
void
Start
()
{
#ifdef USE_OWN_THREADPOOL
m_signalStartIn
.
set_value
();
#endif
}
/**
...
...
@@ -105,7 +152,36 @@ class ThreadPool
*/
void
Join
()
{
#ifdef USE_OWN_THREADPOOL
std
::
for_each
(
m_lstThreads
.
begin
(),
m_lstThreads
.
end
(),
[](
std
::
unique_ptr
<
std
::
thread
>&
pThread
)
{
if
(
pThread
)
pThread
->
join
();
});
#else
m_tp
.
join
();
#endif
}
t_fut
&
GetResults
()
{
return
m_lstFutures
;
}
t_task
&
GetTasks
()
{
return
m_lstTasks
;
}
protected:
void
CallStartFunc
()
{
// ensure that this is only called per-thread, not per-task
std
::
lock_guard
<
std
::
mutex
>
lockStart
(
m_mtxStart
);
thread_local
bool
bThreadAlreadySeen
{
0
};
if
(
m_pThStartFunc
&&
!
bThreadAlreadySeen
)
{
bThreadAlreadySeen
=
1
;
(
*
m_pThStartFunc
)();
}
}
};
...
...
phys/bz.h
View file @
33992f57
...
...
@@ -181,6 +181,7 @@ class Brillouin3D
return
std
::
make_pair
(
true
,
planeperp
);
});
}
poolMiddlePerps
.
Start
();
for
(
auto
&
fut
:
poolMiddlePerps
.
GetResults
())
{
...
...
@@ -218,6 +219,7 @@ class Brillouin3D
}
}
}
poolIntersections
.
Start
();
std
::
size_t
iVertsRemoved
=
0
;
Rt
<
T
,
3
,
8
,
0
>
rt
;
...
...
@@ -272,6 +274,7 @@ class Brillouin3D
return
std
::
make_pair
(
!
bRemoveVertex
,
iVert
);
});
}
poolVerts
.
Start
();
std
::
vector
<
t_vec
<
T
>>
vecNewVertices
;
vecNewVertices
.
reserve
(
m_vecVertices
.
size
());
...
...
@@ -312,6 +315,7 @@ class Brillouin3D
return
std
::
make_tuple
(
bOk
,
vecPoly
,
plane
);
});
}
poolPolys
.
Start
();
for
(
auto
&
fut
:
poolPolys
.
GetResults
())
{
...
...
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