Lots of context switches with shared array

[email protected] (Thomas Karcher) Tue, 23 Sep 2008 17:10:13 +0200
Newsgroups perl.ithreads
Message-ID <1222182613.5444.34.camel@localhost>
--=-Q6MifDRQWRm0mpORpyhL
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

Hi there,

I'm writing a multithreaded merge sort that suffers from massive amounts
of context switches (as seen by vmstat). The outline of the program is
roughly as follows:

=3D=3D=3D=3D
my @sortdata : shared;	// the values to be sorted
[...]
// starting two threads
new threads( \&mergesort_string, $begin, $half, $threaddepth - 1 );
new threads( \&mergesort_string, $half, $end, $threaddepth - 1 );
[...]

sub mergesort_string {
	my ( $begin, $end, $threaddepth ) =3D @_;

	// sorting "my" part of @sortdata, as delimited
	// by $begin and $end
}
=3D=3D=3D=3D

By playing around, I discovered that the "shared" is what causes the
context switches. They take up about 40% of the duration of the program
for an array of 10,000 elements.

I don't need any locking on the elements itself because the merge sort
algorithm itself guarantees in my opinion that there is no conflicting
access to them.

Am I missing something here? How can I avoid those context switches that
make the program essentially useless?


Thanks!

Thomas


--=-Q6MifDRQWRm0mpORpyhL
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: Dies ist ein digital signierter Nachrichtenteil

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkjZBtUACgkQiMyIQtYO79yqXQCg/b/tvQADUfhzSIIbJpmQzO4h
+tcAoIGmM+7ecIipCq/c0Y0UZ9bM6Bo1
=FNaE
-----END PGP SIGNATURE-----

--=-Q6MifDRQWRm0mpORpyhL--