È sicuro un thread unique_ptr? È impossibile che il codice sottostante stampi lo stesso numero due volte?È sicuro il thread unique_ptr?
#include <memory>
#include <string>
#include <thread>
#include <cstdio>
using namespace std;
int main()
{
unique_ptr<int> work;
thread t1([&] {
while (true) {
const unique_ptr<int> localWork = move(work);
if (localWork)
printf("thread1: %d\n", *localWork);
this_thread::yield();
}
});
thread t2([&] {
while (true) {
const unique_ptr<int> localWork = move(work);
if (localWork)
printf("thread2: %d\n", *localWork);
this_thread::yield();
}
});
for (int i = 0; ; i++) {
work.reset(new int(i));
while (work)
this_thread::yield();
}
return 0;
}
"Eccitante, quando e se ciò accade, si otterrà anche il doppio rilascio dell'intero." Potrebbe*. Potrebbe non farlo. Potrebbe risultare non liberare affatto l'intero. Potrebbe portare a entrambe le versioni spostate con metà del valore del puntatore. Potrebbe fare ogni genere di cose. –
Vero, stavo facendo alcune supposizioni sull'architettura che non sono veramente giustificate. – Useless