Example of getting started with multithreaded programming for Windows C + +

Source: Internet
Author: User

[C-sharp]View Plaincopy
  1. #include <windows.h>
  2. #include <iostream>
  3. Using namespace std;
  4. DWORD WINAPI myThread (LPVOID argv); //Declaration of a thread function
  5. int main (int argc, char *argv[])
  6. {
  7. string S;
  8. HANDLE Myhandle;
  9. do{
  10. Cin.clear (); Cin.sync ();
  11. Cin>> s;
  12. switch (s.at (0)) {
  13. case ' 0 ':
  14. Myhandle = CreateThread (Null,0,mythread, (LPVOID) s.c_str (), 0,null); //Create thread
  15. Break ;
  16. case ' 1 ':
  17. if (myhandle) SuspendThread (Myhandle); //Suspend thread
  18. Break ;
  19. case ' 2 ':
  20. if (myhandle) ResumeThread (Myhandle); //Recovery thread
  21. Break ;
  22. case ' 3 ':
  23. if (myhandle) TerminateThread (myhandle,0); //Terminate thread
  24. Break ;
  25. case ' 4 ':
  26. if (myhandle) setthreadpriority (myhandle,thread_priority_normal); //Set thread priority
  27. Break ;
  28. Default:
  29. Break ;
  30. }
  31. cout<< s << Endl;
  32. }while (s!="Exit" && s!= "quit");
  33. return 0;
  34. }
  35. DWORD WINAPI myThread (lpvoid argv) {
  36. int i = 0;
  37. Char buff[200];
  38. ZeroMemory (Buff,sizeof);
  39. memcpy (buff,argv,200);
  40. While (++i) {
  41. cout<< "Extra thread executing with argument string:" << buff <<endl;
  42. Sleep (1000);
  43. if (i = =) ExitThread (0); //exit self thread
  44. }
  45. }

Example of getting started with multithreaded programming for Windows C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.