I am using a mutex for the critical section. I have a design constraint that, 1) If 2 reads occur in 2 different thread, then critical section should be available for both. 2) If there is a write happening, then read on other thread should be blocked 3) If there is a read happening, then write on other thread should be blocked.
I dont know to do 开发者_运维问答this with Createmutex, openmutex, waitforsingleobject and releasemutex apis.
What you are looking for is a Reader-Writer lock. There are some implementations of it on the internet, for example this one.
Take a look at the following link to see whether this native windows api meets your requirements:
Slim Reader/Writer (SRW) Locks
If your synchronization/critical section issue is limited to a single process and many threads, i recommend you use Critical Section Objects instead of Mutex Objects. You have a couple of examples on the pages linked from msdn. Read the linked msdn pages and if you have the basic concurrency synchronization concepts and the required Windows Api knowledge under your belt, you should be rolling in no time, if not read around. There's nothing that special about Mutexes and Critical Sections from the api standpoint.
精彩评论