File tree Expand file tree Collapse file tree 2 files changed +33
-2
lines changed
include/dali/core/exec/tasking Expand file tree Collapse file tree 2 files changed +33
-2
lines changed Original file line number Diff line number Diff line change @@ -35,6 +35,30 @@ TEST(TaskingTest, ExecutorShutdown) {
35
35
});
36
36
}
37
37
38
+ TEST (TaskingTest, ExecutorSetup) {
39
+ /* * Check that setup has effect on all threads */
40
+ static thread_local int tls = 0 ;
41
+ int num_threads = 32 ;
42
+ Executor ex (num_threads);
43
+ ex.Start ([]() { tls = 42 ; }); // set thread-local value
44
+ std::atomic_int correct{0 }, incorrect{0 };
45
+ auto complete = Task::Create ([](){});
46
+ int num_tasks = num_threads * 8 ; // launch a lot of tasks - all taks must see the expected value
47
+ for (int i = 0 ; i < num_tasks; i++) {
48
+ auto task = Task::Create ([&](){
49
+ if (tls == 42 )
50
+ ++correct;
51
+ else
52
+ ++incorrect;
53
+ });
54
+ complete->Succeed (task);
55
+ ex.AddSilentTask (task);
56
+ }
57
+ ex.AddSilentTask (complete);
58
+ ex.Wait (complete);
59
+ EXPECT_EQ (correct, num_tasks);
60
+ EXPECT_EQ (incorrect, 0 );
61
+ }
38
62
39
63
TEST (TaskingTest, IndependentTasksAreParallel) {
40
64
int num_threads = 4 ;
Original file line number Diff line number Diff line change 15
15
#ifndef DALI_CORE_EXEC_TASKING_EXECUTOR_H_
16
16
#define DALI_CORE_EXEC_TASKING_EXECUTOR_H_
17
17
18
+ #include < functional>
18
19
#include < memory>
19
20
#include < thread>
20
21
#include < vector>
@@ -38,13 +39,19 @@ class Executor : public Scheduler {
38
39
/* * Launches the worker threads.
39
40
*
40
41
* Multiple calls to Start have no effect. The function is not thread safe.
42
+ *
43
+ * @param thread_setup A function executed before the main loop.
41
44
*/
42
- void Start () {
45
+ void Start (std::function< void ()> thread_setup = {} ) {
43
46
if (started_)
44
47
return ;
45
48
assert (workers_.empty ());
46
49
for (int i = 0 ; i < num_threads_; i++)
47
- workers_.emplace_back ([this ]() { RunWorker (); });
50
+ workers_.emplace_back ([thread_setup, this ]() {
51
+ if (thread_setup)
52
+ thread_setup ();
53
+ RunWorker ();
54
+ });
48
55
started_ = true ;
49
56
}
50
57
You can’t perform that action at this time.
0 commit comments