AIX: pthread_rwlock_* and writer starvation


 
Thread Tools Search this Thread
Top Forums Programming AIX: pthread_rwlock_* and writer starvation
# 8  
Old 12-10-2009
Quote:
Originally Posted by Corona688
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.
# 9  
Old 12-10-2009
Quote:
Originally Posted by DreamWarrior
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 06:26 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Emergency UNIX and Linux Support

Need an efficient XML writer for Perl

I don't care about user friendliness, but I don't wanna re-invent the wheel either. What's a good XML writer for Perl that's the most efficient? Thanks! (1 Reply)
Discussion started by: stevensw
1 Replies

2. Linux

install openoffice writer from yum

Is there a way to install openoffice writer from yum? Can I add some repository then do that? (5 Replies)
Discussion started by: cokedude
5 Replies

3. Homework & Coursework Questions

awk script that implements a report writer

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The script should compute the sale amounts per associate for the year 2009 and print them in a sorted list ranked... (1 Reply)
Discussion started by: Jeffthrow5
1 Replies

4. Programming

Question about read writer lock

From <<Advanced Programming in the Unix>> section 11.6, it says: Although implementations vary, readerwriter locks usually block additional readers if a lock is already held in read mode and a thread is blocked trying to acquire the lock in write mode. This prevents a constant stream of readers... (5 Replies)
Discussion started by: robin.zhu
5 Replies

5. AIX

Question on CD writer & backup

Hi All, 1. I have many AIX system here but none of them has any CD writer. I want to buy external CD writer so I can move around when needed. Can some one tell me which one they have. Please let me know exact model number so it will be easy for me to order it. I tried going to IBM web site,... (4 Replies)
Discussion started by: samnyc
4 Replies

6. AIX

Probleme with DVD Writer

I can write into DVD? I have USE "MKCD" command mkcd -r directorie -d /dev/cd1 please help me it s urgent (2 Replies)
Discussion started by: mktahar
2 Replies

7. UNIX for Advanced & Expert Users

Probleme With DVD Writer

I Use mkcd for save same directories into DVD, But the commande not complete succefuly mkcd -r directorie_i_whish_save -d /dev/cd1 please it is very urgent thank you :confused: :confused: (1 Reply)
Discussion started by: mktahar
1 Replies

8. UNIX for Dummies Questions & Answers

Share CD-Writer

Hi, guys ! I have a server that runs FreeBSD 5.3 and on that server a have a CD-Writer used for backup. The question is, does anybody know how can I share this CD-Writer ? I want to be able to write CDs from another computer, without transfering all the data to the server and after that use all... (2 Replies)
Discussion started by: Sergiu-IT
2 Replies
Login or Register to Ask a Question