What is the proper way to pass a pointer to 2 threads that each one of the threads runs another operation?
#include <chrono>
#include <iostream>
#include <vector>
#include <thread>
#include <random>
struct Unit {
Unit(uint64_t id_) :
id(id_),
v(1000000000)
{}
uint64_t id;
std::vector<int> v;
};
void operation1(Unit* unit) {
std::cout << "Hello operation1";
}
void operation2(Unit* unit) {
std::cout << "Hello operation2";
}
void operationMain() {
Unit* unit = new Unit(1);
std::thread at1(&operation1, unit);
std::thread at2(&operation2, unit);
at1.detach();
at2.detach();
}
int main123(int argc, char** argv)
{
std::thread t(&operationMain);
t.join();
return 0;
}
Here is my code, and I think I have a memory leak because 'unit' is passing to several operations in different threads.
Any suggestions?
newwithout a correspondingdeleteyou have a memory leak.std::shared_ptr? The rest of the code seems to use C++11 features