ThreadPool icon indicating copy to clipboard operation
ThreadPool copied to clipboard

various improvements in performance, safety, features and readability

Open Youka opened this issue 9 years ago • 9 comments

I tried to keep the original authors style and focused on rather important changes. Everything was tested with the example source but i'd like some impressions too.

Youka avatar Jul 10 '15 18:07 Youka

I agree the for(;;) is more unclear. One idea is to suppress the particular warning for it with #pragma warning (disable: ??). I find it strange that the compiler doesn't issue the same warning for for(;;)

h1arshad avatar Jul 15 '15 06:07 h1arshad

By @wilx

Conditional variables can wake up spuriously and the condition needs to be rechecked on each wakeup.

From cppreference

wait causes the current thread to block until the condition variable is notified or a spurious wakeup occurs, optionally looping until some predicate is satisfied.

Thanks for this hint. I'll delete this commit.

Youka avatar Jul 23 '15 18:07 Youka

@wilx I had not seen your comment when I made my second comment. That is good to know about the spurious wakeups, I didn't know it would do that. Thanks!

JoshuaBrookover avatar Jul 23 '15 20:07 JoshuaBrookover

@Youka nice change! Instead of several commits, can you squash them all as one change so it's easier to look at. Or create separate pull requests for each of the commits.

deepaknc avatar Oct 06 '15 21:10 deepaknc

Suggest to declare ThreadPool as final, instead of introducing virtual destructor.

Calthron avatar Nov 10 '15 21:11 Calthron

Interestingly, Chris Kohlhoff's thread_pool implementation (part of a ISO C++ standards proposal) uses std::thread::hardware_concurrency in its default constructor. I wonder whether that's an oversight or whether it's planned to change the standard.

https://github.com/chriskohlhoff/executors/blob/master/include/experimental/bits/thread_pool.h#L22 The code hasn't been updated in two years though so I'm not sure about the status of that proposal.

/cc @chriskohlhoff

patrikhuber avatar Jun 30 '16 13:06 patrikhuber

Joining in a little late but ...

Simplified for(;;) to while(true) which is more common and less optimizer work

Sorry but I completely disagree. for(;;) has been the idiomatic way of writing an endless loop since C was invented in the stone age. And do you have measurements to back up the "less optimizer work" part?

Love your "role of 5" addition, it's really missing in the original. Also agree with your choice of the ctor default parameter.

wolframroesler avatar Oct 14 '17 12:10 wolframroesler

Sorry but I completely disagree. for(;;) has been the idiomatic way of writing an endless loop since C was invented in the stone age. And do you have measurements to back up the "less optimizer work" part?

I would be surprised if it doesn't generate the exact same assembly code. And indeed I think it does: https://godbolt.org/g/SLuFaP

patrikhuber avatar Oct 14 '17 15:10 patrikhuber

@patrikhuber you are right, it will result in the same executable code. I understood "less optimizer work" to mean that @Youka thought compilation would be faster. I'm pretty sure it won't be (at least, not in a measurable way).

wolframroesler avatar Oct 14 '17 15:10 wolframroesler