Hello,
I've been learning about threads recently and tried to make a function that would create and return a thread given an instance of a class where there are the cond, the mutex, and the variables for my thread. The problem is when creating one thread with this function, the program either crashes immediately after running or runs smoothly but then crashes on close. And when I try to create two threads with it it seems to be stuck in the following loop,
I've tried changing many things in my code but to no avail and tried to look around but not much luck either there. So here is the code in my thread.cpp file excluding headers, etc:
The class I'm using is the same as the one from the tutorial but with more bools. In the main code I have this:
Thanks for the read,
DailyDose
Probably the main problem is that you are passing a copy of the DATA object in your create_thread function. This means the address that is being passed to the Func_Thread actually points to the copy on the stack from the create_thread, which is destroyed as soon as create_thread exits. You should instead pass in a pointer to the data object.
Thanks for the reply, it fixed the problem in less than 5 minutes.