Date: Wed, 14 May 2008 21:50:29 +0300 From: Andriy Gapon <avg@icyb.net.ua> To: freebsd-threads@freebsd.org, freebsd-stable@freebsd.org Subject: Re: thread scheduling at mutex unlock Message-ID: <482B3475.6060803@icyb.net.ua> In-Reply-To: <482B0297.2050300@icyb.net.ua> References: <482B0297.2050300@icyb.net.ua>
next in thread | previous in thread | raw e-mail | index | archive | help
on 14/05/2008 18:17 Andriy Gapon said the following: > I am trying the small attached program on FreeBSD 6.3 (amd64, > SCHED_4BSD) and 7-STABLE (i386, SCHED_ULE), both with libthr as threads > library and on both it produces "BROKEN" message. > > I compile this program as follows: > cc sched_test.c -o sched_test -pthread > > I believe that the behavior I observe is broken because: if thread #1 > releases a mutex and then tries to re-acquire it while thread #2 was > already blocked waiting on that mutex, then thread #1 should be "queued" > after thread #2 in mutex waiter's list. > > Is there any option (thread scheduler, etc) that I could try to achieve > "good" behavior? > > P.S. I understand that all this is subject to (thread) scheduler policy, > but I think that what I expect is more reasonable, at least it is more > reasonable for my application. > > Daniel Eischen has just kindly notified me that the code (as an attachment) didn't make it to the list, so here it is inline. #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h> pthread_mutex_t mutex; int count = 0; static void * thrfunc(void * arg) { while (1) { pthread_mutex_lock(&mutex); count++; if (count > 10) { fprintf(stderr, "you have a BROKEN thread scheduler!!!\n"); exit(1); } sleep(1); pthread_mutex_unlock(&mutex); } } int main(void) { pthread_t thr; #if 0 pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP); pthread_mutex_init(&mutex, &attr); #else pthread_mutex_init(&mutex, NULL); #endif pthread_create(&thr, NULL, thrfunc, NULL); sleep(2); pthread_mutex_lock(&mutex); count = 0; printf("you have good thread scheduler\n"); pthread_mutex_unlock(&mutex); return 0; } -- Andriy Gapon
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?482B3475.6060803>