c++11 執行緒 原子操作 sleep 獲取時間
阿新 • • 發佈:2019-01-30
原子操作:
#include <atomic>
atomic_boolatomic_intatomic_long
執行緒:
#include <thread>
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; }
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; }
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; }
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; }
this_thread::sleep_for( chrono::milliseconds(100) );
獲取時間:
#include<chrono>
system_clock::now()