Код: Выделить всё
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace boost::interprocess;
struct shared_data{
interprocess_mutex mtx;
list qqueue;
};
void modify_list(shared_data*& sdat);
int main(){
shared_memory_object shm(open_or_create, "shm", read_write);
shm.truncate(sizeof(shared_data));
mapped_region reg(shm, read_write);
void* address=reg.get_address();
shared_data* shptr=new (address) shared_data;
vector
processors;
size_t num_processors=4;
for(int j=1; jqqueue.push_back(j);
}
for(size_t i=0; i0){
processors.push_back(pid);
}
}
for (pid_t cpu : processors) {
int status;
waitpid(cpu, &status, 0);
}
}
void modify_list(shared_data*& sdat){
while(true){
sdat->mtx.lock();
if(sdat->qqueue.empty()){
sdat->mtx.unlock();
break;
}
int ff = sdat->qqueue.front();
sdat->qqueue.pop_front();
sdat->mtx.unlock();
cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79284881/c-shared-memory-among-forked-processes-what-do-i-do-wrong[/url]
Мобильная версия