Threads
So I’ve been taking 167/9 and working on this project called Threads. It’s totally bitchy. Which isn’t to say that it’s not fun, ‘cuz it is.<p/>
We have to modify a database application to be multithreaded. That part was pretty easy – using a read/write lock we would lock the database, access a piece of it, then unlock it. That, however, isn’t good enough. The database is stored as a bst, so we had to use fine-grained locking on individual nodes of the database in order to lock it properly. That was also annoying but fortunately not that tough to get right (or, at least, what appears to be right; I can’t tell whether it is for sure).<p/>
After this is all the cancellation stuff. In case you don’t know, POSIX threads have an awesome (but very complicated) feature called cancellation. If someone else wants to cause your thread to terminate, they send a “cancellation request”. For cancellation requests, you can either ignore them, defer them until a cancellation point, or screw it and execute them right this second. Generally you don’t want to do the latter, because chances are good that it’s in a weird state (say, you’ve just locked a wondrous mutex) and everybody dies.<p/>
So generally you use deferral. The places you defer them to are called “cancellation points” and they’re inside most syscalls that block, plus pthread_testcancel
, but not pthread_mutex_lock
. What happens when you get canceled? Well, it executes the cancellation stack and then terminates. The cancellation stack is a series of functions that the thread sets up that say “if we get cancelled here, execute this”. It’s a stack because one outer function might wrap a mutex grab with a cancellation unlock, and then some inner function might use a malloc and want to free it if the thread is canceled. Et cetera.<p/>
Anyway, this stuff isn’t that hard, but it’s very tricky to get right. I have been wading through some nasty bugs when my cancellation stack isn’t set up right, mainly because it’s very tough to figure out where the thread was when it was canceled.<p/>