Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- template <class M = std::string, class E = int>
- E log(M msg, E error = 0)
- {
- std::cout << msg;
- return error;
- }
- class DisplayThread
- {
- public:
- void operator()()
- {
- //std::cout << "Current thread ID = " << std::this_thread::get_id() << "\n";
- for (int i = 0; i < 10; i++)
- log("<OBJECTS>\n");
- }
- };
- void thread_func()
- {
- //std::cout << "Current thread ID = " << std::this_thread::get_id() << "\n";
- for (int i = 0; i < 10; i++)
- log("<POINTER>\n");
- }
- int main()
- {
- // use Function Pointer !
- std::thread threadFuncPointer(thread_func);
- // use Function Objects !
- std::thread threadFuncObjects((DisplayThread()));
- // use Lambda functions !
- std::thread threadFuncLambda([]()
- {
- //std::cout << "Current thread ID = " << std::this_thread::get_id() << "\n";
- for (int i = 0; i < 10; i++)
- log("<LAMBDA>\n");
- });
- for (int i = 0; i < 100; i++)
- log("<MAIN>\n");
- std::cout << "Pointer ID = " << threadFuncPointer.get_id() << "\n";
- std::cout << "Objects ID = " << threadFuncObjects.get_id() << "\n";
- std::cout << "Lambda ID = " << threadFuncLambda.get_id() << "\n";
- threadFuncPointer.join();
- threadFuncObjects.join();
- threadFuncLambda.join();
- std::cout << "Current thread ID = " << std::this_thread::get_id() << "\n";
- return log("- End of main()\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment