Skip to content
Advertisement

pointer get wrong value in different thread

I am writing a piece of code to demonstrate the multi-threading share memory writing.

However, my code gets a strange 0xffffffff pointer I can’t make out why. I haven’t been writing cpp code for a while. please let me know if I get something wrong.

I compile with the command: g++ --std=c++11 shared_mem_multi_write.cpp -lpthread -g

I get error echoes like:

JavaScript

my os is CentOS Linux release 7.6.1810 (Core) gcc version 4.8.5 and the code is posted below:

JavaScript

Advertisement

Answer

You forgot specify access rights for created SHM segment (http://man7.org/linux/man-pages/man2/shmget.2.html):

The value shmflg is composed of:

In addition to the above flags, the least significant 9 bits of shmflg specify the permissions granted to the owner, group, and others. These bits have the same format, and the same meaning, as the mode argument of open(2). Presently, execute permissions are not used by the system.

Change

JavaScript

into

JavaScript

It works for me now: https://wandbox.org/permlink/Am4r2GBvM7kSmpdO


Note that I use only a vector of threads (no shared pointers), as other suggested in comments. You can possibly reserve its space as well.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement