int main(int argc, char** argv) { Create monday; Create tuesday;
printf("At the first God made the heaven and the earth.\n"); monday.start("Naming the light, Day, and the dark, Night, the first day."); tuesday.start("Gave the arch the name of Heaven, the second day."); printf("These are the generations of the heaven and the earth.\n");
return 0; } 编译运行,程序输出如下: At the first God made the heaven and the earth. These are the generations of the heaven and the earth. 令人惊奇的是,由周一和周二对象创建的子线程似乎并没有执行!这是为什么呢?别急,在最后的printf语句之前加上如下语句: monday.wait(); tuesday.wait(); 重新编译运行,新的输出如下: At the first God made the heaven and the earth. Naming the light, Day, and the dark, Night, the first day. One day past. Gave the arch the name of Heaven, the second day. One day past. These are the generations of the heaven and the earth.
void Thread::stop() { if (started && !detached) { TerminateThread(threadHandle, 0);
//Closing a thread handle does not terminate //the associated thread. //To remove a thread object, you must terminate the thread, //then close all handles to the thread. //The thread object remains in the system until //the thread has terminated and all handles to it have been //closed through a call to CloseHandle CloseHandle(threadHandle); detached = true; } }
void Thread::sleep(unsigned int delay) { ::Sleep(delay); }