Threading, mutexes, tokens...

看板DFBSD_kernel作者時間21年前 (2005/02/03 20:01), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/4 (看更多)
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!
文章代碼(AID): #120XA900 (DFBSD_kernel)
文章代碼(AID): #120XA900 (DFBSD_kernel)