- #include <windows.h>
- #include <iostream>
- using namespace std;
- DWORD WINAPI myThread(LPVOID argv);//声明一个线程函数
- int main(int argc, char *argv[])
- {
- string s;
- HANDLE myHandle;
- do{
- cin.clear(); cin.sync();
- cin>> s;
- switch(s.at(0)){
- case '0':
- myHandle = CreateThread(NULL,0,myThread,(LPVOID)s.c_str(),0,NULL);//创建线程
- break;
- case '1':
- if(myHandle) SuspendThread(myHandle);//挂起线程
- break;
- case '2':
- if(myHandle) ResumeThread(myHandle);//恢复线程
- break;
- case '3':
- if(myHandle) TerminateThread(myHandle,0);//终止线程
- break;
- case '4':
- if(myHandle) SetThreadPriority(myHandle,THREAD_PRIORITY_NORMAL);//设定线程优先级
- break;
- default:
- break;
- }
- cout<< s << endl;
- }while(s!="exit" && s!= "quit");
- return 0;
- }
- DWORD WINAPI myThread(LPVOID argv){
- int i = 0;
- char buff[200];
- ZeroMemory(buff,sizeof(buff));
- memcpy(buff,argv,200);
- while(++i){
- cout<< "Extra thread executing with argument string:"<< buff <<endl;
- Sleep(1000);
- if(i == 40) ExitThread(0);//退出自身线程
- }
- }
Windows 下 C/C++ 多线程编程入门参考范例
最新推荐文章于 2024-08-30 11:32:38 发布