質問

I had a question about reader/writer threads and I didn't want to over-complicate things so I came here for some advice.

I have an array that will ultimately be read by 4 threads and written to by 3. (1 reader 3 read/write). My goal was to somehow not have any read operation block another thread while having any write operation block all threads until finished. I'm pretty sure I know how to do this with one semaphore per thread but is there any way to accomplish this with just one binary, counting, or mutex semaphore (or something less than 1 semaphore per thread). I will be using the VxWorks semaphore library.

役に立ちましたか?

解決

Unless VxWorks supports some kind of shared/rwlock like the POSIX one, I am afraid it is going to be a bit more complicated than just simple.

I would suggest using POSIX pthread_rwlock_t, if you have it. If you don't and VxWorks does not offer any equivalent alternative, you will have to build one using other primitives. It is possible to build a rwlock on top of 2 semaphores and 3 mutexes and two integer variables. See Concurrent Control with "Readers" and "Writers"; P.J. Courtois, F. Heymans, and D.L. Parnas; MBLE Research Laboratory; Brussels, Belgium.

他のヒント

Since you have only one reader it is exactly equivalent to mutual exclusion problem (only one thread can be in). Directly using single binary semaphore should be enough. Init with one, P on entering, V on leaving.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top