Advertisement
Guest User

Untitled

a guest
Dec 6th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3.  
  4. void thread_started(std::string name, std::string function, std::string filename, int line)
  5. {
  6.  
  7. }
  8.  
  9. class thread_t : public std::thread
  10. {
  11.    public:
  12.        thread_t() noexcept {}
  13.        thread_t( thread_t&& other ) noexcept : std::thread(static_cast<std::thread&&>(std::move(other))) {}
  14.        template<class function_t, class... args_t >
  15.  
  16. // Following constructor actually does not compile:
  17.        explicit thread_t(std::string name, std::string function, std::string filename, int line, function_t&& f, args_t&&... args ) :
  18.        std::thread(std::bind([name, function, filename, line](function_t&& f, auto&&... args){
  19.            thread_started(name, function, filename, line);
  20.            f(std::forward(args)...);
  21.        }, std::move(f), std::move(args)...)) { }
  22.        thread_t(const thread_t&) = delete;
  23. };
  24. #define THREAD_NAME(name) name, __FUNCTION__, __FILE__, __LINE__
  25.  
  26. int main()
  27. {
  28.     thread_t my_thread(THREAD_NAME("sample thread"), [](){
  29.         // Do actual job
  30.        std::cout << "Hello!";
  31.     });
  32.     std::cout << "Hello, world!\n";
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement