What to pass to thread for dynamic memory | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What to pass to thread for dynamic memory

Hello I came across a question about thread and heap memory : What should be passed to thread function ? 1. shared pointer 2. Raw pointer 3. Shared pointer with reference 4. Any other better choice

29th May 2022, 4:29 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 Answers
+ 1
Generally it is good to avoid anything shared when working with threads, unless the shared resource is carefully guarded to prevent race conditions and gridlock. I cast my vote for "2. raw pointer" so the thread may own the allocated memory exclusively.
29th May 2022, 3:53 PM
Brian
Brian - avatar
0
Thanks.... AFIK, heap is not safe across the threads.... stack is threadsafe but not heap... So , is it not safe either we pass it as shared or raw... correct or Am i missing something ?
29th May 2022, 5:25 PM
Ketan Lalcheta
Ketan Lalcheta - avatar