#include <bits/stdc++.h>
#include <unistd.h>
using namespace std;
#define CLONE
mutex m;
int subproc(void*){
cout << "Start" << endl;
while(true){
vector<int>V;
//m.lock();
V.reserve(10);
//m.unlock();
for(int i=0;i<10;i++)
V.push_back(i);
}
}
int main(){
const unsigned int proc_num = 4;
const unsigned int stack_size = 16*1024*1024;
#if defined CLONE
for(int i=0;i<proc_num;i++)
clone(subproc, new char[stack_size]+stack_size, CLONE_VM, nullptr);
#elif defined THREADS
vector<thread>Vec;
for(int i=0;i<proc_num;i++)
Vec.push_back(thread(subproc, nullptr));
#endif
while(true)
sleep(1);
return 0;
}
When I compile this code with different #defines (CLONE
or THREADS
), program behaves differently. With CLONE
, child processes immediately crash (address sanitizer gives heap-use-after-free
error). When I use THREADS
, everything works as expected.
If I remove comments from mutexes, both versions work well, so the problem is about allocating memory for vector.
Why is that so? And is there a method to make CLONE version work (condition is that all processes should share memory)?