From owner-freebsd-threads@FreeBSD.ORG Mon Nov 7 07:12:01 2005 Return-Path: X-Original-To: freebsd-threads@freebsd.org Delivered-To: freebsd-threads@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 4E0CD16A41F for ; Mon, 7 Nov 2005 07:12:01 +0000 (GMT) (envelope-from nkoch@demig.de) Received: from server.absolute-media.de (server.absolute-media.de [213.239.231.9]) by mx1.FreeBSD.org (Postfix) with ESMTP id 952E743D45 for ; Mon, 7 Nov 2005 07:11:58 +0000 (GMT) (envelope-from nkoch@demig.de) Received: from localhost (unknown [127.0.0.1]) by server.absolute-media.de (Postfix) with ESMTP id 37964A2867; Mon, 7 Nov 2005 08:11:57 +0100 (CET) Received: from server.absolute-media.de ([127.0.0.1]) by localhost (server [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 03533-05; Mon, 7 Nov 2005 08:11:53 +0100 (CET) Received: from firewall.demig (p508392D1.dip0.t-ipconnect.de [80.131.146.209]) by server.absolute-media.de (Postfix) with ESMTP id 2719BA2C1D; Mon, 7 Nov 2005 08:11:53 +0100 (CET) Received: from entw-pr (entw-pr.w2kdemig [192.168.1.40]) by firewall.demig (8.13.5/8.13.4) with ESMTP id jA77AUjj015243; Mon, 7 Nov 2005 08:10:30 +0100 (CET) (envelope-from nkoch@demig.de) X-Originating-IP: [192.168.1.72] From: "Norbert Koch" To: "Nicolas Blais" , "Erich Dollansky" In-Reply-To: <200511061918.00437.nb_root@videotron.ca> Date: Mon, 07 Nov 2005 08:10:30 +0100 Message-ID: <20051107.lED.26049900@entw-pr> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 8bit Content-Disposition: inline X-Mailer: AngleMail for eGroupWare (http://www.egroupware.org) v 1.0.0.009 X-Virus-Scanned: by amavisd-new X-Virus-Scanned: by amavisd-new at absolute-media.de Cc: freebsd-threads@freebsd.org Subject: Re: c++ with pthread? X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 07 Nov 2005 07:12:01 -0000 Hi. If you want to have classes with threads, I suggest you try the port devel/commoncpp. It is a portable c++ library for multi-threading. From my experience it works fine. Norbert Nicolas Blais (nb_root@videotron.ca) schrieb: > > On November 5, 2005 11:07 pm, Erich Dollansky wrote: > > Hi, > > > > Nicolas Blais wrote: > > > It compiles and works fine. I'm trying to make the same program in C++. > > > Is the syntax for pthreading the same as in C? I was wondering if someone > > > can point me in the right direction. > > > > the threading stuff stays the same. > > > > > Also, is there anything I should be worried about when using pthread and > > > C++? (Some years ago, I heard that FreeBSD's pthread didn't like C++... > > > could be a false rumour) > > > > You cannot use classes directly with threading functions. Your classes > > must be aware of threading. You need some mechanism to protect classes > > which are not threadsafe if more than one thread could access an object > > at te same moment of time. > > > > This is a general problem of threading which might causes problems. > > > > Do not forget that C++ uses additional data in objects which the > > programmer does not see. > > > > Erich > > //warn long message > > Well, at least it's feasible. > > Actually, so far I've converted an single threaded application which was > helped by pthreading so I'm on the right track. (Instead of performing a > calculation 5+E07 times, I've split it in 'n' threads that does part of the > calculation. Threading gives a small performance boost on my 64/3800 but I'm > sure it would be even more on a dual-core or multiprocessor system). > > Now my current problem is that it uses a class and like you mentionned, and > the threads access the same data within the class. This is the desired > operation for most of my variables except one which I want to be thread > independant. > > For example: Let's say I have 4 threads performing 1000 calculations. Each > thread adds +1 to the global counter so that at the end of the program I have > 4000 in that counter. I cannot yet have an individual counter per thread so > that at the end of the program, each thread's counter = 1000. > > I've tried using an array like : array[thread_id] which works fine for small > numbers, but as soon as I get into large numbers (way into 'double'), I > occasionnaly get corrupt numbers. The data doesn't get corrupted below a > counter of around 1+E6. I'm guessing the threads are adding in each other's > "individual" counters. > > From what I found, pthread_key_create() could be what I need, but I haven't > found one C++ implementation that could get me to understand it. I did find > many C example, but they either heavily depend on malloc(), casts and such or > they don't compile at all (some are 5 years old Solaris stuff). > > Can anyone show me the correct path? > > Any help is truly appreciated. > -- > FreeBSD 7.0-CURRENT #0: Sat Nov 5 12:12:36 EST 2005 > root@clk01a:/usr/obj/usr/src/sys/CLK01A > PGP? : http://www.clkroot.net/security/nb_root.asc >