Googles appar
Huvudmeny

Post a Comment On: cbloom rants

"02-24-09 - Low Level Threading - Part 3.0"

4 Comments -

1 – 4 of 4
Anonymous Anonymous said...

The try-discard loop is only like a spinlock if you're using it in the same way.

The important point about a try-discard loop is that if you succeed, then you know that you've made some progress, and just as important, if you fail, you know that someone *someone else has made some progress* (otherwise the global ptr/whatever that you're upating wouldn't have changed -- unless your algo is broken of course). Therefore, whatever happens, *someone* makes progress, and therefore it's a lock free algo.

If you use a spin-lock to do that, then you've achieved pretty much the same thing. Of course there's another little problem though because if you use any kind of lock to do it (spinlock or not), then you run the risk of things like the OS pre-empting your thread just after it's acquired the lock, and ending up blocking *everyone* because now you're not doing anything and no one else can get the lock. If you're updating with a CAS then that obviously can't happen -- you can't be pre-empted *during* the CAS, only before or after it, and in either case everyone else gets to carry on and update stuff while you're blocked.

February 24, 2009 at 9:48 PM

Blogger cbloom said...

Yeah, funny I wrote something very similar for part 4.0

BTW this makes me wonder if when you do use a traditional mutex/critsec - should you bump your thread priority to infinite when you hold it? It seems like in well designed code that holds a critsec for a very short period, getting thread-switched inside the critsec is a pretty huge disaster.

February 25, 2009 at 12:09 PM

Anonymous Anonymous said...

Interesting idea. I'd kind of assume changing thread priority will have bad overheads (at least bad for this type of use) due to the scheduler messing with its book-keeping data, but maybe not. Do you have any info/guesses about that?

February 25, 2009 at 3:30 PM

Blogger cbloom said...

My tests indicate that SetThreadPriority is actually super cheap - it appears to just set the value in your thread info block. The scheduler doesn't actually see that you've changed until it gets invoked by something (a Wait or a timer interrupt).

I can't say for sure though. And it is by no means a cheap function call because it does jump into kernel mode, so it would add a lot of overhead to critsecs.

February 25, 2009 at 3:57 PM

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.