1 2 3 4 5 6 7 8 9 10 11 12 13 | int funcReturnInt(const char* fmt, ...){ va_list ap; va_start(ap, fmt); vprintf( fmt, ap ); va_end(ap); return 0xabcd; } void threadRunFunction(void){ thread* t = new thread(funcReturnInt, "%d%s\n", 100, "\%"); t->join(); delete t; } 我们也可以传入一个 Lambda 表达式作为入口,比如: |
1 2 3 4 5 6 7 8 9 10 11 | void threadRunLambda(void){ int a = 100, b = 200; thread* t = new thread( [](int ia, int ib){ cout << (ia + ib) << endl; }, a, b ); t->join(); delete t; } |
1 2 3 4 5 6 7 8 9 10 11 | struct God{ void create(const char* anything){ cout << "create " << anything << endl; } }; void threadRunMemberFunction(void){ God god; thread* t = new thread( &God::create, god, "the world" ); t->join(); delete t; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | static void on_signal_term(int sig){ cout << "on SIGTERM:" << this_thread::get_id() << endl; pthread_exit(NULL); } void threadPosixKill(void){ signal(SIGTERM, on_signal_term); thread* t = new thread( [](){ while(true){ ++counter; } }); pthread_t tid = t->native_handle(); cout << "tid=" << tid << endl; // 确保子线程已经在运行。 this_thread::sleep_for( chrono::seconds(1) ); pthread_kill(tid, SIGTERM); t->join(); delete t; cout << "thread destroyed." << endl; } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |