Hello
I'm working with MFC program which receives UDP data and parses it to images.
There are three additional threads (beside main thread):
1. receiving UDP data by using ReceiveFrom() and saving the data into buffer
2. reading buffer and writing it to text file by fprintf()
3. reading buffer and parsing/writing it into image file by fwrite().
Sometimes (including when other processes start to run) there is some data loss both in text file and image file,
so I think it was a loss in ReceiveFrom().
In order to resolve the problem, I would like to set higher priority to thread 1.
Now, there is no priority set. I'm using just CreateThread().
In referring literature, I found PROCESS_MODE_BACKGROUNG_END as a parameter in SetPriorityClass().
(and THREAD_MODE_BACKGROUND_BEGIN in SetThreadPriority())
I think it might be important because my project is all about I/O.
But I have no idea about effect of the parameter and even of the priority set.
I want to understand how it works.
Can you advise me regarding priority set and whether you think that can solve the problem?
----- added -----
Global variables:
#define SIZE_BUFFER 50
#define SIZE_PACKET 414
static char buffer_received[SIZE_BUFFER][SIZE_PACKET];
static unsigned int in, out_log, out_image;
Thread 1 is running:
char received[SIZE_PACKET];
while (1)
{
int recvMsgSize = udpServer.ReceiveFrom(received, SIZE_PACKET, senderIP, senderPort);
mu.lock();
if (in + 1 < SIZE_BUFFER)
memcpy_s(buffer_received[in + 1], SIZE_PACKET, received, SIZE_PACKET);
else
memcpy_s(buffer_received[0], SIZE_PACKET, received, SIZE_PACKET);
in++;
if (in == SIZE_BUFFER)
in = 0;
mu.unlock();
}
Thread 2 is running:
while (1)
{
if (in != out_log)
{
out_log++;
if (out_log == SIZE_BUFFER)
out_log = 0;
memcpy_s(buffer, SIZE_PACKET, buffer_received[out_log], SIZE_PACKET);
stringToHexa_WiresharkForm(buffer, SIZE_PACKET, buffer_hex);
Decode_NavData(buffer);
fprintf(ptr_logFile, "%s\n\n", buffer_hex);
count++;
if (count > 5000)
{
fflush(ptr_logFile);
count = 0;
}
}
}
Thread 3 is running:
while (1)
{
if (in != out_image)
{
out_image++;
if (out_image == SIZE_BUFFER)
out_image = 0;
memcpy_s(buffer, SIZE_PACKET, buffer_received[out_image], SIZE_PACKET);
if (!Decode_Image(buffer))
return 0;
}
}
The code above is the core part of each thread. (unnecessary codes were removed)
I think the important things are timing of increment of variable 'in' and writing to 'buffer_received'. But the timing is well treated I think.
Sometimes, in one of reading (thread 1) and writing (thread 2, 3) routine, there is data loss.
The UDP data is being sent from other device in a bunch of 414 bytes (called packet), and 400 packets per second.
If neither of synchronization and priority is the problem, I think it is the design of the code.
1. read buffer size in ReceiveFrom() ?
2. while loop design ?
I'm doubting above two, but have no solution.