Google face questions-there are four threads 1, 2, 3, 4. The function of thread 1 is output 1, the function of thread 2 is output 2, etc... There are now four file ABCD

Source: Internet
Author: User

Category: Windows programming C + + 2012-10-27 19:56 3410 people read reviews (1) favorite reports There are four threads of 1, 2, 3, 4. The function of thread 1 is output 1, the function of thread 2 is output 2, etc... There are now four file ABCD. The initial is empty. Now you want four files to appear in the following format: A:1 2 3 4 1 2....b:2 3 4 1 2 3....c:3 4 1 2 3 4....d:4 1 2 3 4 1 .... Please design the program. [CPP]View Plaincopy
  1. #include <stdio.h>
  2. #include <process.h>
  3. #include <windows.h>
  4. #include <fstream.h>
  5. #include <string.h>
  6. unsigned int __stdcall fun (void *ppm);
  7. Number of threads
  8. const INT thread_num = 4;
  9. const INT file_num = 4;
  10. Ofstream Ofile[file_num];
  11. The next file to be written for a thread
  12. int file_thread[file_num]={0,1,2,3};
  13. int next_loop[file_num]={0,1,2,3,};
  14. Critical_section G_csfile;
  15. Number of Cycles
  16. const INT LOOP = 6;
  17. Mutex events
  18. HANDLE G_hthreadevent[thread_num];
  19. int main ()
  20. {
  21. printf ("\ t has four threads of 1, 2, 3, 4.  The function of thread 1 is output 1, the function of thread 2 is output 2\n ");
  22. printf ("And so on ...). There are now four file ABCD. The initial is empty.  Four files are to be presented in the following format \ n ");
  23. int i = 0, J;
  24. HANDLE Hdl[thread_num];
  25. InitializeCriticalSection (&g_csfile);
  26. //When the event is turned on, there is no trigger.
  27. For (i = 0; i < thread_num; i++)
  28. G_hthreadevent[i] = CreateEvent (null,false,false,null);
  29. //The thread number is passed in as a thread parameter, first converted to a pointer, then converted to shaping,
  30. char file_thread[] ="A.txt";
  31. For (i = 0; i < file_num; i++)
  32. {
  33. File_thread[0] = i + ' A ';
  34. Ofile[i].open (File_thread,ios::trunc);
  35. if (Ofile[i].fail ())
  36. {
  37. printf ("failed to open file%s", File_thread);
  38. continue;
  39. }
  40. }
  41. For (i = 0; i < thread_num; i++)
  42. Hdl[i] = (HANDLE) _beginthreadex (Null,0,fun, (void*) i,0,null);
  43. //triggers the first thread, and the thread function triggers the next thread to execute on its own
  44. SetEvent (G_hthreadevent[0]);
  45. WaitForMultipleObjects (Thread_num,hdl,true,infinite);
  46. //Cleanup
  47. For (i = 0; i < thread_num; i++)
  48. {
  49. CloseHandle (Hdl[i]);
  50. CloseHandle (G_hthreadevent[i]);
  51. }
  52. For (i = 0; i< file_num;i++)
  53. {
  54. Ofile[i].close ();
  55. }
  56. DeleteCriticalSection (&g_csfile);
  57. return 0;
  58. }
  59. unsigned int __stdcall fun (void *ppm)
  60. {
  61. int num = (int) PPM;
  62. int i = 0;
  63. For (i = 0; i< loop;i++)
  64. {
  65. //wait sequence trigger
  66. WaitForSingleObject (G_hthreadevent[num],infinite);
  67. EnterCriticalSection (&g_csfile);
  68. printf ("thread%d is writing to the%c file, and the next time it operates on file%c is thread%d\n",
  69. num + 1, File_thread[num] + ' A ', File_thread[num] + ' A ', (num+1)% (thread_num) + 1);
  70. ofile[file_thread[num]]<<num+1<<"";
  71. Sleep (200);
  72. //Record the next round corresponding serial number to manipulate the file, (num+1)% (file_num) event corresponding to the thread, action file File_thread[num]
  73. next_loop[(num+1)% (file_num)] = File_thread[num];
  74. if (num + 1 = = File_num)
  75. {
  76. printf ("\ n");
  77. //This write end, will calculate the next round of file operation sequence to take over
  78. memcpy (&file_thread,&next_loop,file_num *sizeof (int));
  79. }
  80. LeaveCriticalSection (&g_csfile);
  81. //Trigger the next thread, 1 trigger 2, 2 trigger 3,3 trigger 1
  82. SetEvent (g_hthreadevent[(num+1)%thread_num]);
  83. }
  84. return 0;
  85. }


Google face questions-there are four threads 1, 2, 3, 4. The function of thread 1 is output 1, the function of thread 2 is output 2, etc... There are now four file ABCD

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.