I'm new to c++ and have a little problem.
So I have a UdpServeur which receives messages and I would like another program I wrote to use them. So I used FIFO tube but it's very heavy for the code. Then I used locks with a simple .txt file but it's not very clean and not good for the project.
For my project people told me that I can use a list with a reference but I didn't really understand.
I'm pretty sure there is a very very simple way to make the programs interact with the same list without a lot of effort... Please Help me ! :D Thank you very much.
Using lock guard is not complex, and you even do not need to care about releasing it.
std::mutex list_mutex;
// Access from thread 1:
{
std::lock_guard guard(list_mutex);
// Work with the list
}
// Access from thread 2:
{
std::lock_guard guard(list_mutex);
// Work with the list
}
Both list and list_mutex must be declared somewhere where both threads could see them. As soon as the execution leaves the curved brace area (also using return or throwing exceptions), the lock guard would be released.