10-02-2023 06:56 AM
Hi everyone, I'm having some problem with an application so I've decided to make a small example of what I want to do.
In a few line, i'm acquiring data in the main thread and i want to save this to a file without interferring the acquisition... yeah, i know, you will ask why didn't you aquire the signal in multithread and save in the main? because i'm doing a state machine and it's easyer to manage it in the main thread, we are working like this from a lot of year and change will be more expensive.
obviously the example works fine, so I cannot reproduce my problem, but if you look at the variable in debug step by step maybe you will understand what happen.
the debugger when i free the first pointer lost the reference to the next one and the software crash, I repeat that in this example works but the development is almost the same in my application.
i upload the example if you want to check it.
10-13-2023 09:40 AM
Hello,
you struct examples initializers should be
struct example ThreadSafeQueueValue = {0,0,NULL,NULL};
and your int should not be {0}, but that's not the problem.
When I first ran your program it did work, but then never again ! How's that for a race condition... 😜
I try to avoid multithreading in C for good reasons ! And when I have to I usually use the POSIX interface, not CVI's which I always had difficulty to understand. I guess somebody else can chime in on that...
Did you start from one of the examples provided by CVI ?
10-13-2023 10:02 AM
Thanks for you reply.
if you initialize at {0} i saw that the compiler put everything to 0, so is the same of doing {0,0,NULL,NULL}... this happen only to me?
I think that the problem in this software is not caused by a race condition, but it's a problem with malloc() and free() not managed between two thread... I'm not so expert in this field and I was hoping to find someone more than me.
nope, this is a thread management that i usually do in my software. I've started from the example in LabView regarding the producer-consumer loop and tryed to replicate something similar in CVI.
I think it's good designed and useful, for example I use the thread to continous read slow devices with RS485 modbus and get the data passed by the queue, in this way the software will not be locked waiting a response.
Or opposite, I also use it for device that are really fast and will cause too many interrupt in my software making it unusable; it will do the mangament of the message in a thread and then I read the queue when I get a desired number of elements.