Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add StartThread type checking wrapper #8303

Closed
wants to merge 4 commits into from

Conversation

Glebanister
Copy link
Contributor

  • Add class FunctorWrapper to invoke the function with given parameters
  • Add is_invocable type trait
  • Implement WrapStartThread which wraps StartThread with type checking cover
  • Demonstrate WrapStartThread in test util/thread_local_test.cc

#8285

@pdillinger pdillinger self-requested a review May 17, 2021 17:43
using FWType = FunctorWrapper<Args...>;
StartThread(
[](void* arg) {
auto* functor = reinterpret_cast<FWType*>(arg);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use reinterpret_cast to cast from void* (in new code)

// Start a new thread, invoking "function(args...)" within the new thread.
// When "function(args...)" returns, the thread will be destroyed.
template <typename FunctionT, typename... Args>
auto WrapStartThread(FunctionT function, Args&&... args) ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just void return type without all the type trait stuff? The definition is inline and always will be because it's such a general template, so bad instantiations will fail to compile based on the (small) function definition being ill-formed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but I thought the error message would be a little nicer if the compiler failed to substitute function in the template at the entry to the WrapStartThread (or StartThreadTypeSafe) than if it failed to substitute a function in the constructor of std::function. The only difference is the size of the error. If it's not that important, I'll delete it, of course:)

// Start a new thread, invoking "function(args...)" within the new thread.
// When "function(args...)" returns, the thread will be destroyed.
template <typename FunctionT, typename... Args>
auto WrapStartThread(FunctionT function, Args&&... args) ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being "wrapped" is an implementation detail. The key feature vs. StartThread is type safety. Perhaps StartThreadTyped

@facebook-github-bot
Copy link
Contributor

@pdillinger has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@pdillinger pdillinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@facebook-github-bot
Copy link
Contributor

@pdillinger merged this pull request in 748e3ac.

Copy link
Contributor

@ajkr ajkr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat PR, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants