syedshan
Advanced Member level 1
- Joined
- Feb 27, 2012
- Messages
- 463
- Helped
- 27
- Reputation
- 54
- Reaction score
- 26
- Trophy points
- 1,308
- Location
- Jeonju, South Korea
- Activity points
- 5,134
hello every one,
I am receiveing very fast DMA data from FPGA based on PCIe protocol, so I have to save them in file in .txt format and I try to make the protoype of it but it does not work at all. Note that I am usually the FPGA programmer and not much use the c++ and this is the very first time using threads so kinda stuck in here.
So in this prototype, I am inilizating the Global variable pointer (much like I decide to do in the actual thing) and then passing data to it, simulatnously I am activating flags which will activate the file saving in the respective threads. please see the code below. Unfortunately I am unsuccessful in writing to both files.
Moreover I am getting the following error as well (see image below)
Note please find the attached files as well.
I am receiveing very fast DMA data from FPGA based on PCIe protocol, so I have to save them in file in .txt format and I try to make the protoype of it but it does not work at all. Note that I am usually the FPGA programmer and not much use the c++ and this is the very first time using threads so kinda stuck in here.
So in this prototype, I am inilizating the Global variable pointer (much like I decide to do in the actual thing) and then passing data to it, simulatnously I am activating flags which will activate the file saving in the respective threads. please see the code below. Unfortunately I am unsuccessful in writing to both files.
Moreover I am getting the following error as well (see image below)
Note please find the attached files as well.
Code:
#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);
}