[C-sharp]View Plaincopy
- #include <windows.h>
- #include <iostream>
- Using namespace std;
- DWORD WINAPI myThread (LPVOID argv); //Declaration of a thread function
- 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); //Create thread
- Break ;
- case ' 1 ':
- if (myhandle) SuspendThread (Myhandle); //Suspend thread
- Break ;
- case ' 2 ':
- if (myhandle) ResumeThread (Myhandle); //Recovery thread
- Break ;
- case ' 3 ':
- if (myhandle) TerminateThread (myhandle,0); //Terminate thread
- Break ;
- case ' 4 ':
- if (myhandle) setthreadpriority (myhandle,thread_priority_normal); //Set thread priority
- 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);
- memcpy (buff,argv,200);
- While (++i) {
- cout<< "Extra thread executing with argument string:" << buff <<endl;
- Sleep (1000);
- if (i = =) ExitThread (0); //exit self thread
- }
- }
Example of getting started with multithreaded programming for Windows C + +