#define BUF_SIZE 255
#define size_of_memory 40
typedef signed short int ssint;
ssint *pInDMA = (ssint *)_aligned_malloc( sizeof(ssint)*(size_of_memory)*2, 4096);
int flag_oper = 0;
/*Function to save file */
DWORD WINAPI save_uwpi_file0( LPVOID )
{
//signed short *mem;
ssint *Lmem; int w_cnt = 0;
FILE *opfile;
char fname[30] = "txt_file0.txt";
//opening file for write
opfile = fopen(fname , "w");
do{
printf("assigning memory for file 1 \n");
Lmem = (ssint *) pInDMA;
for( int nbr = 0; nbr < size_of_memory; nbr++){
fprintf(opfile , "%hi\n", Lmem[nbr] );
}
printf("aligned free 1\n");
_aligned_free(Lmem);
fclose(opfile);
printf("File saved 1\n\n");
return 1;
} while ((flag_oper & 0x01) != 0x01);
}
/*Function to save file */
DWORD WINAPI save_uwpi_file1( LPVOID )
{
//signed short *mem;
ssint *Lmem2; int w_cnt = 0;
FILE *opfile;
char fname[30] = "txt_file1.txt";
//opening file for write
opfile = fopen(fname , "w");
do{
printf("assigning memory for file 2 \n");
Lmem2 = (ssint *) pInDMA;
for( int nbr = 0; nbr < size_of_memory; nbr++){
fprintf(opfile , "%hi\n", Lmem2[nbr] );
}
_aligned_free(Lmem2);
fclose(opfile);
printf("File saved 2\n\n");
return 1;
}
while((flag_oper & 0x02) != 0x02) ;
}
// Main thread, for memory buffer data writing
// This causes the other threads to write in the files
DWORD WINAPI mem_data_gen( LPVOID) {
int cnt = 0;
int gt = 0;
for (cnt = 0; cnt < 2; cnt ++ ){
for (int x =0 ;x < size_of_memory; x++)
pInDMA[x] = x*(cnt+1);
gt ++;
switch(gt) {
case 1 : { flag_oper = 0x01; break; }
case 2 : { flag_oper = 0x02; break; }
}
}
return 1;
}
void main()
{
// Aray to store thread handles
HANDLE Array_Of_Thread_Handles[3] = {0, 0, 0};
// Create thread 1 (mem_data_gen).
Array_Of_Thread_Handles[0] = CreateThread( NULL, 0, mem_data_gen, NULL, 0, NULL);
//if ( Array_Of_Thread_Handles[0] == NULL) ExitProcess(Data_Of_Thread_1);
// Create thread 2 (save_uwpi_file0).
Array_Of_Thread_Handles[1] = CreateThread( NULL, 0, save_uwpi_file0, NULL, 0, NULL);
//if ( Array_Of_Thread_Handles[1] == NULL) ExitProcess(Data_Of_Thread_2);
// Create thread 2 (save_uwpi_file1).
Array_Of_Thread_Handles[2] = CreateThread( NULL, 0, save_uwpi_file1, NULL, 0, NULL);
//if ( Array_Of_Thread_Handles[1] == NULL) ExitProcess(Data_Of_Thread_2);
// Wait until all threads have terminated.
WaitForMultipleObjects( 3, Array_Of_Thread_Handles, TRUE, INFINITE);
printf("Since All threads executed lets close their handles \n");
// Close all thread handles upon completion.
CloseHandle(Array_Of_Thread_Handles);
}