Date: Sat, 13 Aug 2022 20:31:12 +0200
> On 12 Aug 2022, at 18:24, David Carter via Std-Discussion <std-discussion_at_[hidden]> wrote:
>
> It's something like this:
>
> for (size_t i=0; i < numThreads; ++) {
> threadPool_.push_back(std::thread(&ClassName::ClassFn, this, arg1, arg2, arg3));
> }
> . . .
> queue work
> . . .
> condition_variable stuff
>
> threadPool_ is a private member of ClassName and of type std::vector<std::thread>
I use a lambda to pass to the thread-pool, below shown with Boehm GC registration. The latter puts locks on each allocation, so may not be thread effective. The function eval_thread then picks tasks off a priority queue.
data& C::eval_main(D& dr, size_type n) {
std::vector<std::thread> threads;
// Create up to N threads:
for (int k = 0; k < N; ++k)
threads.push_back(std::thread(
// Lambda wrap for 'eval_thread' with GC register/unregister:
[this, &dr, n] {
// Register thread with GC:
GC_stack_base sb;
GC_get_stack_base(&sb);
GC_register_my_thread(&sb);
eval_thread(dr, n);
// Unregister thread with GC:
GC_unregister_my_thread();
}));
// Joining threads.
for (auto& i: threads)
i.join();
return data_;
}
>
> It's something like this:
>
> for (size_t i=0; i < numThreads; ++) {
> threadPool_.push_back(std::thread(&ClassName::ClassFn, this, arg1, arg2, arg3));
> }
> . . .
> queue work
> . . .
> condition_variable stuff
>
> threadPool_ is a private member of ClassName and of type std::vector<std::thread>
I use a lambda to pass to the thread-pool, below shown with Boehm GC registration. The latter puts locks on each allocation, so may not be thread effective. The function eval_thread then picks tasks off a priority queue.
data& C::eval_main(D& dr, size_type n) {
std::vector<std::thread> threads;
// Create up to N threads:
for (int k = 0; k < N; ++k)
threads.push_back(std::thread(
// Lambda wrap for 'eval_thread' with GC register/unregister:
[this, &dr, n] {
// Register thread with GC:
GC_stack_base sb;
GC_get_stack_base(&sb);
GC_register_my_thread(&sb);
eval_thread(dr, n);
// Unregister thread with GC:
GC_unregister_my_thread();
}));
// Joining threads.
for (auto& i: threads)
i.join();
return data_;
}
Received on 2022-08-13 18:31:15