I'm using the pthread library on Linux.
I'm assigning a string in thread A, and then trying to print the string in thread B. However, the string just prints out empty (I have verified that it works in thread A).
Note: The string resides inside an object, which I suspect may be getting cleaned up or re-instantiated empty... The container object doesn't give me a seg fault or anything, just all the values are empty.
Is this because threads cannot access memory from other threads, or because the memory is being unallocated once thread A stops? Or is it neither; it could well be a bug in my code, but I just wanted to rule this out...
Turns out it was a memory issue. With thanks to your answers, I have also answered this my self, please do comment on my answer if you agree/disagree.
Threads, unlike processes, share a common memory space inside a process (each thread has its own stack, but heaps are typically shared). Therefore, when you quit a thread, memory allocated from the shared heap is not freed automatically. But, for example, if you have allocated a string object on the stack and passed it somewhere by a simple pointer, the destructor will free the memory when the thread exits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With