From owner-freebsd-hackers@FreeBSD.ORG Fri Feb 21 06:05:55 2014 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 539F05BE; Fri, 21 Feb 2014 06:05:55 +0000 (UTC) Received: from alogt.com (alogt.com [69.36.191.58]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 25ADF14FB; Fri, 21 Feb 2014 06:05:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=alogt.com; s=default; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Subject:Cc:To:From:Date; bh=G7kNjGIYsD/DnFwe4D3MsyoGNjKeb6nL9Z/ZVeCtj5U=; b=pGxQ+U+yTGS+J2sNPUA1bJjdVOoCpkWg3WlYsMXeWqd5A3e6lx9grfg1rwXld1NZXRYTJyd8DgW5PvdEbYfup4uJQWn/8eZDC2hD6rVSblGXjq7CXPFT9bPVzW9xoyHK9J0oq+dIty/ny4ulurV0h5X/RNzamLQ3KzlDWOjZWZ4=; Received: from [182.9.34.57] (port=37793 helo=X220.alogt.com) by sl-508-2.slc.westdc.net with esmtpsa (SSLv3:DHE-RSA-AES128-SHA:128) (Exim 4.82) (envelope-from ) id 1WGjFB-004GQs-Tp; Thu, 20 Feb 2014 23:05:54 -0700 Date: Fri, 21 Feb 2014 14:05:41 +0800 From: Erich Dollansky To: David Xu Subject: Re: pthread programming eats up resources (My or FreeBSD's fault?) Message-ID: <20140221140541.72020e93@X220.alogt.com> In-Reply-To: <5306E70D.70402@freebsd.org> References: <20140218180646.GA67861@schlappy> <53059574.8090605@freebsd.org> <20140220140644.7b1e0074@X220.alogt.com> <5305B786.8020708@freebsd.org> <20140221124405.0791d375@X220.alogt.com> <5306DCC8.3080001@freebsd.org> <20140221131517.66d5b82c@X220.alogt.com> <5306E70D.70402@freebsd.org> X-Mailer: Claws Mail 3.9.3 (GTK+ 2.24.22; amd64-portbld-freebsd10.0) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - sl-508-2.slc.westdc.net X-AntiAbuse: Original Domain - freebsd.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - alogt.com X-Get-Message-Sender-Via: sl-508-2.slc.westdc.net: authenticated_id: erichsfreebsdlist@alogt.com X-Source: X-Source-Args: X-Source-Dir: Cc: freebsd-hackers@freebsd.org, Andre Albsmeier X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 21 Feb 2014 06:05:55 -0000 Hi, On Fri, 21 Feb 2014 13:41:33 +0800 David Xu wrote: > On 2014/02/21 13:15, Erich Dollansky wrote: > > On Fri, 21 Feb 2014 12:57:44 +0800 > > David Xu wrote: > > > I am running your code while playing flash video, and many threads are it is not even my code. I am just interested. > accumulated, sometimes I saw a peak number at 800 or more, I think > it is normal, because the machine is loaded, after usleep returns, > other newly created threads may still not have chance to run, and > your next loop creates bunch of threads again, it could be very > easily to exceed 1000 threads. > > > Should I add some code to wait until the counter is zero again and > > see what is happening then? > > > > Yes, I think so. But thread stacks will be cached by libthr, and never > return to free space. so even if there is only one thread, you may > see a very large size in top. > I first called usleep only of more than 10 threads remained active. Top showed then nicely that this happens sometimes. Since I call usleep only when more than 1000 threads remain active, looks even funnier on top. I added then a loop to the thread and it works smoothly now. It runs now also into problems creating new threads when too many are still running. Of course, the program takes now much more CPU time as it waits less. I think that we can say that the library is not really optimised for empty threads. So, all seems perfect now. Thanks for your work. Erich