#include <windows.h> #include <process.h> #include <iostream> #include <fstream> using namespace std; HANDLE evRead, evFinish; void ReadThread(LPVOID param) { WaitForSingleObject (evRead ,INFINITE); cout<<"Reading"<<endl; SetEvent (evFinish); } void WriteThread(LPVOID param) { cout<<"Writing"<<endl; SetEvent (evRead); } int main(int argc , char * argv[]) { evRead = CreateEvent (NULL ,FALSE ,FALSE ,NULL) ; evFinish = CreateEvent (NULL ,FALSE ,FALSE ,NULL) ; _beginthread(ReadThread , 0 , NULL) ; _beginthread(WriteThread , 0 , NULL) ; WaitForSingleObject (evFinish,INFINITE) ; cout<<"The Program is End"<<endl; return 0 ; }
时间: 2024-03-29 13:35:33 浏览: 99
使用include <iostream>时一定要加using namespace std
这段代码是一个简单的多线程程序,使用了Windows API。其中,CreateEvent函数用于创建两个事件对象evRead和evFinish,_beginthread函数用于创建两个线程ReadThread和WriteThread。主函数中,先等待evFinish事件被触发,然后输出程序结束的消息。在线程函数中,ReadThread等待evRead事件被触发后输出"Reading",并触发evFinish事件;WriteThread直接输出"Writing",并触发evRead事件。这个程序的运行结果是先输出"Writing",然后输出"Reading",最后输出"The Program is End"。
阅读全文