有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD。初始都为空。现要让四个文件呈如下格式: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....请设计程序。
#include <stdio.h>
#include <process.h>
#include <windows.h>
#include <fstream.h>
#include <string.h>
unsigned int __stdcall Fun(void *pPM);
//线程个数
const int THREAD_NUM = 4;
const int FILE_NUM = 4;
ofstream ofile[FILE_NUM];
//某个线程的下一个要写的文件
int FILE_THREAD[FILE_NUM]={0,1,2,3};
int NEXT_LOOP[FILE_NUM]={0,1,2,3,};
CRITICAL_SECTION g_csFile;
//循环次数
const int LOOP = 6;
//互斥事件
HANDLE g_hThreadEvent[THREAD_NUM];
int main()
{
printf("\t有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2\n");
printf("以此类推.........现在有四个文件ABCD。初始都为空。现要让四个文件呈如下格式\n");
int i = 0 , j;
HANDLE hdl[THREAD_NUM];
InitializeCriticalSection(&g_csFile);
//事件开启的时候是没有触发的
for (i = 0; i < THREAD_NUM; i++)
g_hThreadEvent[i] = CreateEvent(NULL,FALSE,FALSE