Myforwik
Myforwik

Reputation: 3588

MapViewOfFile with pointers between threads

I have some programs that use MapViewOfFile to share data, but I am getting strange access violations that seem to be from accessing the mapped file data.

Some of the shared data has pointers, however these pointers are only set and used by one process, but by several threads within the process.

I understand that you can't use pointers in mapped view across different processes, as obviously they could be mapped to different memory for each process, but is it safe to use pointers in mapped memory between threads on the same process?

Upvotes: 1

Views: 1051

Answers (2)

PapaBoojum
PapaBoojum

Reputation: 11

You can share pointers among threads within the same process. Just make sure you protect the shared memory with a lock, such as a critical section. Simultaneous access of the shared memory by multiple threads - especially if one or more of the threads are updating the memory - can easily cause access violations.

Upvotes: 1

user200783
user200783

Reputation: 14348

Yes, it is safe to share pointers (in mapped memory or not) between threads in the same process, since the threads share the same address space.

Upvotes: 0

Related Questions