开发者

CreateMutex in two different programs, same handle (gcc)

开发者 https://www.devze.com 2023-04-05 14:38 出处:网络
I have written two programs which comunicate over tcp. These two programs are in principle the same. The messages are stored in an internal buffer, before i send开发者_Python百科 them over tcp. I use

I have written two programs which comunicate over tcp. These two programs are in principle the same. The messages are stored in an internal buffer, before i send开发者_Python百科 them over tcp. I use a mutex to protect this buffer for simultaneous read/write. For testing I did run these two programs on the same pc and got some timing problems. I found out that CreateMutex always returns the same handle on both programs and so one program is blocking the other one!

hBufferMutex = CreateMutex(NULL,FALSE,"BufferMutex");

It doesn't matter which name I use for the mutex, it is always the same on both programs. What am I doing wrong?


If you are just using the mutex within a program, use an anonymous one - set name to NULL. Name is really only used when you want to communicate between two programs (...running on the same login session, which is the default, or same machine, if you use a 'global\' prefix.)

In your case, you might be able to use a Critical Section instead of a mutex - it's basically a more lightweight version that can only be used within a process.

0

精彩评论

暂无评论...
验证码 换一张
取 消