From owner-freebsd-threads@FreeBSD.ORG Mon Nov 7 00:18:02 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 7CB8816A41F for ; Mon, 7 Nov 2005 00:18:02 +0000 (GMT) (envelope-from nb_root@videotron.ca) Received: from relais.videotron.ca (relais.videotron.ca [24.201.245.36]) by mx1.FreeBSD.org (Postfix) with ESMTP id 2242F43D46 for ; Mon, 7 Nov 2005 00:18:02 +0000 (GMT) (envelope-from nb_root@videotron.ca) Received: from clk01a ([66.130.198.54]) by VL-MO-MR002.ip.videotron.ca (Sun Java System Messaging Server 6.2-2.05 (built Apr 28 2005)) with ESMTP id <0IPK00J2A660GAK0@VL-MO-MR002.ip.videotron.ca> for freebsd-threads@freebsd.org; Sun, 06 Nov 2005 19:18:01 -0500 (EST) Date: Sun, 06 Nov 2005 19:17:52 -0500 From: Nicolas Blais In-reply-to: <436D8181.9090909@pacific.net.sg> To: Erich Dollansky Message-id: <200511061918.00437.nb_root@videotron.ca> MIME-version: 1.0 Content-type: multipart/signed; boundary=nextPart1210774.aLU6Faaqad; protocol="application/pgp-signature"; micalg=pgp-sha1 Content-transfer-encoding: 7bit References: <200511051927.40752.nb_root@videotron.ca> <436D8181.9090909@pacific.net.sg> User-Agent: KMail/1.8.3 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 00:18:02 -0000 --nextPart1210774.aLU6Faaqad Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline 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++.= =20 > > Is the syntax for pthreading the same as in C? I was wondering if someo= ne > > 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. =20 Actually, so far I've converted an single threaded application which was=20 helped by pthreading so I'm on the right track. (Instead of performing a=20 calculation 5+E07 times, I've split it in 'n' threads that does part of the= =20 calculation. Threading gives a small performance boost on my 64/3800 but I'= m=20 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= =20 the threads access the same data within the class. This is the desired=20 operation for most of my variables except one which I want to be thread=20 independant. =46or example: Let's say I have 4 threads performing 1000 calculations. Eac= h=20 thread adds +1 to the global counter so that at the end of the program I ha= ve=20 4000 in that counter. I cannot yet have an individual counter per thread so= =20 that at the end of the program, each thread's counter =3D 1000. I've tried using an array like : array[thread_id] which works fine for smal= l=20 numbers, but as soon as I get into large numbers (way into 'double'), I=20 occasionnaly get corrupt numbers. The data doesn't get corrupted below a=20 counter of around 1+E6. I'm guessing the threads are adding in each other's= =20 "individual" counters. =46rom what I found, pthread_key_create() could be what I need, but I haven= 't=20 found one C++ implementation that could get me to understand it. I did find= =20 many C example, but they either heavily depend on malloc(), casts and such = or=20 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. =2D-=20 =46reeBSD 7.0-CURRENT #0: Sat Nov 5 12:12:36 EST 2005 =20 root@clk01a:/usr/obj/usr/src/sys/CLK01A=20 PGP? : http://www.clkroot.net/security/nb_root.asc --nextPart1210774.aLU6Faaqad Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (FreeBSD) iD8DBQBDbp04z38ton5LGeIRAuouAJwPewuWEIpX0QDV/iGjn96t6GEzAACeNKcR 2OK3cAVCryik/bLvv2yuexc= =I/RQ -----END PGP SIGNATURE----- --nextPart1210774.aLU6Faaqad--