?? detached.c
字號:
#include <thread.h>#include <iostream>int exitCond=0;using namespace cpp_threads;class simple : public Pthread {protected: Semaphore m_sem;public: simple() { }; ~simple() { } int thread(void *) { std::cout << "thread started." << std::endl; while (!exitCond) { m_sem.wait(); std::cout << "now doing some major tasks" << std::endl; sleep(5); } std::cout << "phew... ending" << std::endl; return 0; } Semaphore& sem() { return m_sem; };};intmain(){ simple *th = new simple(); std::cout << "Threads are like variables in a main program" << std::endl; std::cout << "so when the main program ends, so do the threads" << std::endl; std::cout << "as like any variable, it is cleaned up on exit." << std::endl; std::cout << "To make a thread independant of the main program," <<std::endl; std::cout << "set the pthread::set_detached to true." << std::endl; th->run(); // tell the thread to run ahead sleep(1); // Make the thread an independant process. th->set(Pthread::set_detached_e,true); th->sem().post(); // Thread to do some tasks. sleep(5); th->sem().post(); exitCond = 1; exit(0);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -