I'm wondering how to do this (using C++98). Here's my scenario: I have process A and process B. Process A allocates a large buffer in shared memory and splits it into a fixed number of chunks. It then uses a series of structs like these to represent each chunk:

```
struct Chunk
{
    Lock lock; //wrapper for pthread_attr_t and pthread_mutex_t
    char* offset; //address of the beginning of this chunk in the shared memory buffer
};

```

The Lock when constructed does this:

```
pthread_mutexattr_init(&attrs);
pthread_mutexattr_setpshared(&attrs, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&lock,&attrs); //lock is pthread_mutex_t and attrs is pthread_mutexattr_t

```

the lock method when called does this:

```
pthread_mutex_lock(&lock);

```

It uses placement new when creating the above "Chunk"s into the beginning of the shared memory buffer like this:

```
char* mem; //pointer to the shared memory
Chunks[i] = new (mem) Chunk; //for i = {1..num chunks}
mem += sizeof(Chunk);

```

Then it assigns offsets and proceeds to write into the rest of the buffer through it's lifetime. Each time it is writing in the chunk corresponding to one of the aboves it grabs the chunks lock and releases it when done.

Now process B comes up and maps the same shared memory buffer into memory and attempts to retrieve the chunks like this:

```
Chunk** chunks = reinterpret_cast<Chunk**)(mem); //mem being the pointer into the shared memory

```

then it tries to operate on the shared memory by scanning around the different chunks and also trying to make use of the lock if needed.

I am getting strange crashes when I run this in which the chunks\*\* is garbage and am wondering if the Lock would work across the processes as well or if there are any other caveats I'm ignoring in the simple steps above? Is having the SHARED pthread attr enough or do I need to use an entirely different breed of lock?