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
- #include <stdio.h>
- #include <process.h>
- #include <windows.h>
- #include <fstream.h>
- #include <string.h>
- unsigned int __stdcall fun (void *ppm);
- Number of threads
- const INT thread_num = 4;
- const INT file_num = 4;
- Ofstream Ofile[file_num];
- The next file to be written for a thread
- int file_thread[file_num]={0,1,2,3};
- int next_loop[file_num]={0,1,2,3,};
- Critical_section G_csfile;
- Number of Cycles
- const INT LOOP = 6;
- Mutex events
- HANDLE G_hthreadevent[thread_num];
- int main ()
- {
- 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 ");
- 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 ");
- int i = 0, J;
- HANDLE Hdl[thread_num];
- InitializeCriticalSection (&g_csfile);
- //When the event is turned on, there is no trigger.
- For (i = 0; i < thread_num; i++)
- G_hthreadevent[i] = CreateEvent (null,false,false,null);
- //The thread number is passed in as a thread parameter, first converted to a pointer, then converted to shaping,
- char file_thread[] ="A.txt";
- For (i = 0; i < file_num; i++)
- {
- File_thread[0] = i + ' A ';
- Ofile[i].open (File_thread,ios::trunc);
- if (Ofile[i].fail ())
- {
- printf ("failed to open file%s", File_thread);
- continue;
- }
- }
- For (i = 0; i < thread_num; i++)
- Hdl[i] = (HANDLE) _beginthreadex (Null,0,fun, (void*) i,0,null);
- //triggers the first thread, and the thread function triggers the next thread to execute on its own
- SetEvent (G_hthreadevent[0]);
- WaitForMultipleObjects (Thread_num,hdl,true,infinite);
- //Cleanup
- For (i = 0; i < thread_num; i++)
- {
- CloseHandle (Hdl[i]);
- CloseHandle (G_hthreadevent[i]);
- }
- For (i = 0; i< file_num;i++)
- {
- Ofile[i].close ();
- }
- DeleteCriticalSection (&g_csfile);
- return 0;
- }
- unsigned int __stdcall fun (void *ppm)
- {
- int num = (int) PPM;
- int i = 0;
- For (i = 0; i< loop;i++)
- {
- //wait sequence trigger
- WaitForSingleObject (G_hthreadevent[num],infinite);
- EnterCriticalSection (&g_csfile);
- printf ("thread%d is writing to the%c file, and the next time it operates on file%c is thread%d\n",
- num + 1, File_thread[num] + ' A ', File_thread[num] + ' A ', (num+1)% (thread_num) + 1);
- ofile[file_thread[num]]<<num+1<<"";
- Sleep (200);
- //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]
- next_loop[(num+1)% (file_num)] = File_thread[num];
- if (num + 1 = = File_num)
- {
- printf ("\ n");
- //This write end, will calculate the next round of file operation sequence to take over
- memcpy (&file_thread,&next_loop,file_num *sizeof (int));
- }
- LeaveCriticalSection (&g_csfile);
- //Trigger the next thread, 1 trigger 2, 2 trigger 3,3 trigger 1
- SetEvent (g_hthreadevent[(num+1)%thread_num]);
- }
- return 0;
- }
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