Open 3 threads, the IDs of these 3 threads are a, B, C, each thread will have its own ID on the screen, and the output must be displayed in the order of ABC: ABCABC

Source: Internet
Author: User

Category: Windows programming C + + 2012-10-27 15:38 3270 people read reviews (6) favorite reports The fourth question (Thunder written question): Write a program, open 3 threads, the IDs of the 3 threads are a, B, C, each thread will print its own ID on the screen 10 times, the output must be displayed in the order of ABC, such as: Abcabc .... recursion in turn. [CPP]View Plaincopy
  1. #include <stdio.h>
  2. #include <process.h>
  3. #include <windows.h>
  4. #define G_HTHREADEVENT (n) g_hthreadevent# #1
  5. unsigned int __stdcall fun (void *ppm);
  6. Number of threads
  7. const INT thread_num = 8;
  8. Number of Cycles
  9. const INT LOOP = 6;
  10. Mutex events
  11. HANDLE G_hthreadevent[thread_num];
  12. int main ()
  13. {
  14. printf ("\ t sub-thread loops 10 times, then the main thread loops 100 times, then goes back to the child thread loop 10 times, \ n then goes back to the main thread and loops 100 times, so loop 50 times \ n");
  15. int i = 0, J;
  16. HANDLE Hdl[thread_num];
  17. For (i = 0; i < thread_num; i++)
  18. G_hthreadevent[i] = CreateEvent (null,false,false,null);
  19. //The thread number is passed in as a thread parameter, first converted to a pointer, then converted to shaping,
  20. For (i = 0; i < thread_num; i++)
  21. Hdl[i] = (HANDLE) _beginthreadex (Null,0,fun, (void*) i,0,null);
  22. SetEvent (G_hthreadevent[0]);
  23. WaitForMultipleObjects (Thread_num,hdl,true,infinite);
  24. For (i = 0; i < thread_num; i++)
  25. {
  26. CloseHandle (Hdl[i]);
  27. CloseHandle (G_hthreadevent[i]);
  28. }
  29. return 0;
  30. }
  31. unsigned int __stdcall fun (void *ppm)
  32. {
  33. int num = (int) PPM;
  34. int i = 0;
  35. For (i = 0; i< loop;i++)
  36. {
  37. WaitForSingleObject (G_hthreadevent[num],infinite);
  38. //printf ("Thread id=%d, thread number is%c,%d \ n", GetCurrentThreadID (), num+ ' A ');
  39. printf ("%c", num+' A ');
  40. if (num+1 = = thread_num)
  41. printf ("\ n");
  42. SetEvent (g_hthreadevent[(num+1)%thread_num]);
  43. }
  44. return 0;
  45. }

Open 3 threads, the IDs of these 3 threads are a, B, C, each thread will have its own ID on the screen, and the output must be displayed in the order of ABC: ABCABC

Related Article

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.