Googles appar
Huvudmeny

Post a Comment On: cbloom rants

"02-23-13 - Threading APIs that would be ideal"

5 Comments -

1 – 5 of 5
Blogger Unknown said...

You might consider http://msdn.microsoft.com/en-us/library/windows/desktop/Hh706898(v=vs.85).aspx (wait on address) somewhere in your taxonomy.

Wake side condition testing is pretty cute, but it might be too hard to implement efficiently. You basically need to have a locked wait list under which you evaluate the condition, stretching out your lock hold time on the wake side. An alternate approach would be to just have more CVs, each of which corresponds to a condition that someone might wait for.

You generally should do the actual waking outside the lock, because thread scheduling is pretty expensive. Windows has the wait morphing optimization for those who signal a cv under an SRW lock, so the scheduling actually occurs when the lock is released.

February 26, 2013 at 4:47 AM

Blogger Unknown said...

You might consider http://msdn.microsoft.com/en-us/library/windows/desktop/Hh706898(v=vs.85).aspx (wait on address) somewhere in your taxonomy.

Wake side condition testing is pretty cute, but it might be too hard to implement efficiently. You basically need to have a locked wait list under which you evaluate the condition, stretching out your lock hold time on the wake side. An alternate approach would be to just have more CVs, each of which corresponds to a condition that someone might wait for.

You generally should do the actual waking outside the lock, because thread scheduling is pretty expensive. Windows has the wait morphing optimization for those who signal a cv under an SRW lock, so the scheduling actually occurs when the lock is released.

February 26, 2013 at 4:48 AM

Blogger cbloom said...

"Wake side condition testing is pretty cute, but it might be too hard to implement efficiently. You basically need to have a locked wait list under which you evaluate the condition, stretching out your lock hold time on the wake side."

Part of the point is that if the OS provides an API like this (rather than emulating it in user space), it could be super efficient.

The wait list is just the list of threads that the OS already has sleeping on that signal, no additional list. The API always has the CV mutex held when that list is checked, so no additional mutex is needed. The only addition is that OS has to test the condition before waking a thread.

But that can only be a performance *win*. It's of course much better to test the condition on the waker-side, rather than just blindly waking it and having it checked by the wakee. You can construct pathological cases, like some thread has a very expensive wait condition and it is at the head of the list and never true - but that same case would be even *worse* with the alternative (wakee-side checking, or out-of-cv checking).

And of course you still wouldn't want to just stick every thread on the same CV, that would be an abuse of the system. It's mainly for cases where you have multiple wait conditions that are sort of interrelated by slightly different; like several threads that might consume the same item but have slightly different conditions to do so.

February 26, 2013 at 8:56 AM

Blogger jfb said...

Hi Charles,

Isn't this essentially the C# Monitor API?

It lets you do loops like:

lock (_instance) {
while (whatever)
{
while (nothing to do) { Monitor.Wait(_instance); }
do.processing;
}
}

February 28, 2013 at 8:57 AM

Blogger cbloom said...

That looks like a standard condvar, doing wake-side condition checking.

February 28, 2013 at 9:06 AM

You can use some HTML tags, such as <b>, <i>, <a>

This blog does not allow anonymous comments.

Comment moderation has been enabled. All comments must be approved by the blog author.

You will be asked to sign in after submitting your comment.