Subject: pthread_create() and more than 1 CPU
To: None <current-users@netbsd.org>
From: Michael Graff <explorer@flame.org>
List: current-users
Date: 12/02/2004 12:22:18
--Boundary-00=_6l3rBCc4EKTg0/q
Content-Type: text/plain;
  charset="us-ascii"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

So, I have a little program I'm using to torture test a bit of code I have to 
make certain I have the locking code right.

I am using pthread_create() to create the threads.  They all start, then 
eventually exit.  However, at no time does ps display more than one LWP in 
use, and at no time does it consume all the CPU resources available on the 
machine.  It seems to use only one CPU.

I have set the undocumented and magical value PTHREAD_CONCURRENCY in my 
environment to 2, and now it runs with 2 LWPs and uses 200% CPU.  Whee.

Would it Really Break anything if I applied this patch?  It will make it so a 
process by default uses ncpu for its maximum concurrency, which really seems 
more in line with every other OS out there.  Other than Solaris, of course, 
where pthread_setconcurrency() must be called to set the limit up to ncpu.  I 
suppose we could implement that as well, but I'd rather see libpthread do 
what most people expect -- use ncpu by default.

--Michael

--Boundary-00=_6l3rBCc4EKTg0/q
Content-Type: text/x-diff;
  charset="us-ascii";
  name="foo.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="foo.diff"

Index: pthread.c
===================================================================
RCS file: /cvsroot/src/lib/libpthread/pthread.c,v
retrieving revision 1.36
diff -u -r1.36 pthread.c
--- pthread.c	12 Aug 2004 10:54:13 -0000	1.36
+++ pthread.c	2 Dec 2004 20:19:56 -0000
@@ -145,7 +145,7 @@
 	pthread__lockprim_init(ncpu);
 
 	/* Find out requested/possible concurrency */
-	pthread__maxconcurrency = 1;
+	pthread__maxconcurrency = ncpu;
 	p = getenv("PTHREAD_CONCURRENCY");
 	if (p)
 		pthread__maxconcurrency = atoi(p);

--Boundary-00=_6l3rBCc4EKTg0/q--