Threading, mutexes, tokens...
Ivan Voras
ivoras at fer.hr
Thu Feb 3 03:37:24 PST 2005
This is a "I'd like to learn it, finally" post :)
So far, I've been coding userland applications (C, Java, scripting
languages...), where I use mutexes to serialize access to variables,
structures, etc. A (simple) example:
struct {
mutex_t lock;
int a;
int b;
} my_struct;
void thread_proc() {
while (something) {
lock_mutex(data.lock);
modify(&data.a);
release_mutex(data.lock);
do_stuff();
}
}
void main() {
thread_t t1 = make_thread(thread_proc); // create&start
thread_t t2 = make_thread(thread_proc);
sleep(1000); // or do something usefull
}
I see this is fairly different (trivially simple?) than situations
reffered to in various posts explaining mutexes and tokens -
specifically, I don't "see" the need for "With a hard mutex model you
have to pass your mutex down into other subsystems that you call so they
can release it before they potentially block, or you have to temporarily
release your mutex yourself, or a myrid of other issues" (quote from
Matt's recent interview). I'm not experienced enough, so could someone
give me a pseudocode-example of this (prefferably in a real-world case?)?
How (and if?) would my example, and the complex one, be changed by using
"tokens"? (prefferably, could someone show me using a similar
pseudocode?) Do Dragonfly tokens have any effect on userspace applications?
Thanks!
More information about the Kernel
mailing list