E
E
Eugene Ordinary2017-01-13 12:51:45
Programming
Eugene Ordinary, 2017-01-13 12:51:45

How to deallocate dynamic memory shared by two threads?

The main thread creates a dynamic array. Then a detached thread is created that uses this dynamic array.

int *arr[N];
...
thread thr(Daughter);
thr.detach(); 
...
delete[] arr;

The child thread periodically checks for a flag that the main thread can set. When the flag is set, the child thread exits.
If the main thread decides to terminate itself and the work of the child thread, sets the child thread's termination flag, and immediately starts freeing memory, then an error may occur, since the child thread does not immediately stop and can access the freed memory.
How and when to free memory?
It should be noted that the child thread itself cannot free the memory, since it can be claimed in the main thread even after the completion of the child.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Silin, 2017-01-13
@byme

This is not C ++, but the essence should be clear.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question