Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming.threads > #845

Mutex required for a simple shared-variable case?

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.alt.net
From Lasse Kliemann <lasse-usenet-2011@mail.plastictree.net>
Newsgroups comp.programming.threads
Subject Mutex required for a simple shared-variable case?
Date Tue, 3 Apr 2012 15:31:23 +0200
Organization Altopia Corp. - Usenet Access - www.altopia.com
Lines 31
Message-ID <5hbjd7.krj.17.1@news.alt.net> (permalink)
User-Agent tin/1.8.3-20070201 ("Scotasay") (UNIX) (Linux/3.3.0-snyder (i686)) tinews.pl/1.1.24
X-PGP-Fingerprint 581B 0B45 B2F9 7CFA 9DAF 818B B5A1 735A 03BB 5FD5
X-PGP-Hash SHA1
X-PGP-Key 0xB5A1735A03BB5FD5
X-PGP-Sig GnuPG-v1.4.11 From,Newsgroups,Subject,Date,X-PGP-Fingerprint,Message-ID iQIcBAEBAgAGBQJPevuuAAoJELWhc1oDu1/VatsQAICpogVgHLyxgW1DcUQkaIft 9U4OnkaS9Z4dUvAi9RG143biFKMbygZkFLsTun2zv2S3XQbnyiF5/oalGRZ5jVay xM6yam0EaQbjvuesbGaWw//KJiv1ZsFb3124Ev9OT/3p5lIbaS+i4FyDphpLsyjF JguId1nEK66XtALWM2zMuES/DBtC7YuFeQKDpoOBz2tPEL7iWDmYVDmzNqhChOiP CRDnmwnns0Q1HeVpgBZJJ5izPdjmZO4gUnc4A3HoS6S27q5lWKEFWBsvhgqpakxm HAFjz8kqHmh7x11XBWLrKCqIVXX7a0eS6F+cxSFoRLylFeEgvyX5u4Npy3sBZtGr hjV05uEpIteKkn0g4s6Q9JCYFWYiYnZGx10nVykgxclX8nHkO4QpQ8oYM5YyoMbN ngBPjLmlwlffcJLaLcMfXjGMLAGDb096ZFmPItFup1Nltwxu+GDt+rhHlThQP/g4 anTZFGrKT4z148VeeqYbPV5kJZBh4/g54drQc5P/ZKvTgt0wu0y9tXrbKzMkXJQ5 3fLpalMklAZmowRBkOdZBGs8pKKmyopViB8WgowDab3r3D1M8XCWzdnArJoNGXFi 4mgNN3Y5s5vMN1oz4Q+4wEzaUv8z15WyuhemVHtz0GYMXF8QUfN9cfWfX3PSxKbg WIjwYFEUx/Ie1KdXz0sS =WM2I
Xref csiph.com comp.programming.threads:845

Show key headers only | View raw


A number of threads execute a loop each. Once one of them finds a 
certain condition, it shall signal all the other threads to stop.

My first idea was to implement this with a shared variable. It is 
passed to each of the threads as a pointer to an integer, say 
'go_on'. It is set to 1 in the beginning, before the threads 
start. If any of the threads finds the stopping condition given, 
it sets this variable to 0 and terminates. Each of the loops 
looks like this:

for(...; ... && (*go_on); ...) { ...

So it will not do another iteration if the place pointed to by 
'go_on' shows 0.

Inside the loop, the stopping condition is checked for:

   if ...; *go_on = 0;

Are there any mutexes required to read or write to the place 
pointed to by 'go_on'?

All resources on thread programming emphasize how important 
mutexes are for shared data. But I do not quite see why it should 
be necessary in such a simple case. And for performance reasons, 
I would rather keep it as simple as possible.

Can someone give me a quick explanation? I would be satisfied 
with a Pthreads-specific answer, since that's what I am using.

Thank you!

Back to comp.programming.threads | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Mutex required for a simple shared-variable case? Lasse Kliemann <lasse-usenet-2011@mail.plastictree.net> - 2012-04-03 15:31 +0200
  Re: Mutex required for a simple shared-variable case? Marcel Müller <news.5.maazl@spamgourmet.com> - 2012-04-04 01:45 +0200
  Re: Mutex required for a simple shared-variable case? Joshua Maurice <joshuamaurice@gmail.com> - 2012-04-05 17:08 -0700

csiph-web