Go Back   The UNIX and Linux Forums > Top Forums > Programming
google site



Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Display Modes
  #8  
Old 12-10-2009
Registered User
 

Join Date: Oct 2003
Posts: 115
Thanks: 0
Thanked 1 Time in 1 Post
Quote:
Originally Posted by Corona688 View Post
I've used a single semaphore for reader/writer locks on occasion.

It's used somewhat like it usually would -- readers call sem_wait, do their business, then call sem_post -- but the semaphore starts with a value higher than one so multiple threads can read without contention. To get a write lock, the writer waits n times, does its business, then posts n times. This does make writing somewhat of a bigger operation than reading but if readers don't hog, writers never starve.
Interesting, but it seems to rely on the sem_post being fair. Also, while the writer is waiting, the longer he waits, the less reader throughput there is because as he gets "deaper" into the wait he hogs more of the reader locks. Which, is I guess how it works because more readers begin blocking....

I also assume that for this to work "well" I need to know ahead of time the maximum number of readers I'll have. Otherwise, if I have more readers than the starting count, all extraneous readers are pretty much pointless.
Sponsored Links
  #9  
Old 12-10-2009
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 3,095
Thanks: 6
Thanked 51 Times in 51 Posts
Quote:
Originally Posted by DreamWarrior View Post
Interesting, but it seems to rely on the sem_post being fair.
True enough. It should be a straightforward queue like a mutex, afaik. The code did operate the same way in BSD, Solaris, and Linux(and was almost alone in that), but I've never built anything inside AIX. sem_post is the only IPC that's actually defined as signal-safe, so POSIX sems are perhaps more strictly defined than other IPC.
Quote:
Also, while the writer is waiting, the longer he waits, the less reader throughput there is because as he gets "deeper" into the wait he hogs more of the reader locks.
True to a point. No matter how you code it(unless you have "stop-the-world" style IPC like in Java) a write lock must block all new readers and wait for all the old readers to finish. But when the writer needs to wait 10 times, there's no guarantee it would get the next 10 waits in a row even when the sem's fair.

It fulfills your requirements, though: No blocking at all occurs whenever a writelock isn't in progress, which makes reader efficiency very high. If POSIX sem's work the way I think they do, it won't starve writers either.

There's also SYSV IPC sems, which can do arbitrary subtraction atomically. I have no idea how fairly it would be treated however.

You could also roll your own sem capable of arbitrary subtraction, though that'd either mean adding one frequently-blocking call per readlock, or building something like a linux futex out of hard ASM. A futex is a half-userspace sem that's extremely efficient in some circumstances since the nonblocking case involves no system calls at all, just 2-3 instructions.

Quote:
I also assume that for this to work "well" I need to know ahead of time the maximum number of readers I'll have. Otherwise, if I have more readers than the starting count, all extraneous readers are pretty much pointless.
You could do one extra post per new thread, and one extra wait per terminating thread if you're careful about it. Or you could set it to a fixed number higher than you need.

Last edited by Corona688; 12-10-2009 at 05:26 PM..
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Question on CD writer & backup samnyc AIX 4 02-27-2009 11:21 PM
Openserver 6 & using a NEC DVD Writer ifleet2007 SCO 2 08-24-2007 08:58 AM
Probleme with DVD Writer mktahar AIX 2 12-04-2005 03:46 AM
Probleme With DVD Writer mktahar UNIX for Advanced & Expert Users 1 11-26-2005 10:42 AM
Share CD-Writer Sergiu-IT UNIX for Dummies Questions & Answers 2 05-12-2005 01:22 PM



All times are GMT -4. The time now is 07:47 AM.