comp.lang.ada
 help / color / mirror / Atom feed
From: mockturtle <framefritti@gmail.com>
Subject: A curiosity...
Date: Thu, 4 Dec 2008 12:47:30 -0800 (PST)
Date: 2008-12-04T12:47:30-08:00	[thread overview]
Message-ID: <a88dafcc-04d0-4a46-a168-7cb034894181@k41g2000yqn.googlegroups.com> (raw)

Dear all,
this is not a question, nor an announcement, but
just something that I discovered few days ago and
I would like to share with you.  I know you can
appreciate it.

Few days ago a formerly student of mine came to
ask me something about (C) threads.  She showed
me a tutorial she found somewhere (unfortunately
I do not know where, so I cannot give you any
reference).

In case you never used the pthread library, let me say
that in order to start a thread you use the function

pthread_create (thread,attr,start,arg)

where
  thr     : opaque variable associated to the thread
  attr    :  thread attributes
  start  :  address of the function executed by the thread
  arg    :  parameter (void*) passed to the thread


In the tutorial they showed how to start several threads,
giving to each one its "ID" (an integer).  First they show
the wrong solution which was something

/*---- Begin Wrong --- */
void *thr_main(void *ID_pt)
{
    int my_ID = *(int *) ID_pt;
   /*--- other stuff... */
}

int main()
{
    int t;
    pthread_t th[10];

    for (t=0; t<10; t++)
     { pthread_create(th+t, NULL, thr_main, (void*) &t); }
}
/*---- End wrong ---*/

They say (correctly) that this is wrong since there is no
guarantee that the thread starts before t is incremented
again.  After the wrong solution they give the right one.

/*---- Begin Right --- */
void *thr_main(void *pt)
{
    int my_ID = (int) pt; /*!!!!!*/
   /*--- other stuff... */
}

int main()
{
    int t;
    pthread_t th[10];

    for (t=0; t<10; t++)
     { pthread_create(th+t, NULL, thr_main, (void*)  t); } /*!!!!!*/
}
/*---- End Right ---*/

In order to pass t "by value" they first convert it to
a pointer to void, then back to an integer...

Excuse me  while I turn the heat up... I suddenly
feel a chill down my spine.... ;-)



             reply	other threads:[~2008-12-04 20:47 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-12-04 20:47 mockturtle [this message]
2008-12-04 21:17 ` A curiosity Adam Beneschan
2008-12-04 21:48   ` Hyman Rosen
2008-12-04 22:04     ` Ludovic Brenta
2008-12-04 22:10       ` Ludovic Brenta
2008-12-04 22:24         ` Hyman Rosen
2008-12-05  8:02         ` Samuel Tardieu
2008-12-05 11:51           ` Peter C. Chapin
2008-12-05 13:09           ` Martin Krischik
2008-12-05 15:15             ` Hyman Rosen
2008-12-06 13:26             ` Peter C. Chapin
2008-12-05 16:37           ` Keith Thompson
2008-12-04 22:19       ` Hyman Rosen
2008-12-05  9:03       ` Georg Bauhaus
2008-12-04 22:30 ` Randy Brukardt
2008-12-04 22:57   ` Hyman Rosen
2008-12-05 16:42     ` Keith Thompson
2008-12-05 16:57       ` Hyman Rosen
2008-12-05 20:20         ` Keith Thompson
2008-12-05 20:59           ` Adam Beneschan
2008-12-06 22:53             ` Hyman Rosen
2008-12-06 23:15               ` Gary Scott
2008-12-08 15:39                 ` Hyman Rosen
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox