Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- void thread_started(std::string name, std::string function, std::string filename, int line)
- {
- }
- class thread_t : public std::thread
- {
- public:
- thread_t() noexcept {}
- thread_t( thread_t&& other ) noexcept : std::thread(static_cast<std::thread&&>(std::move(other))) {}
- template<class function_t, class... args_t >
- // Following constructor actually does not compile:
- explicit thread_t(std::string name, std::string function, std::string filename, int line, function_t&& f, args_t&&... args ) :
- std::thread(std::bind([name, function, filename, line](function_t&& f, auto&&... args){
- thread_started(name, function, filename, line);
- f(std::forward(args)...);
- }, std::move(f), std::move(args)...)) { }
- thread_t(const thread_t&) = delete;
- };
- #define THREAD_NAME(name) name, __FUNCTION__, __FILE__, __LINE__
- int main()
- {
- thread_t my_thread(THREAD_NAME("sample thread"), [](){
- // Do actual job
- std::cout << "Hello!";
- });
- std::cout << "Hello, world!\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement