From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,7b65fc33a05f24b6,start X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!k41g2000yqn.googlegroups.com!not-for-mail From: mockturtle Newsgroups: comp.lang.ada Subject: A curiosity... Date: Thu, 4 Dec 2008 12:47:30 -0800 (PST) Organization: http://groups.google.com Message-ID: NNTP-Posting-Host: 83.103.77.217 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1228423651 16272 127.0.0.1 (4 Dec 2008 20:47:31 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 4 Dec 2008 20:47:31 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: k41g2000yqn.googlegroups.com; posting-host=83.103.77.217; posting-account=9fwclgkAAAD6oQ5usUYhee1l39geVY99 User-Agent: G2/1.0 X-HTTP-UserAgent: Opera/9.61 (X11; Linux i686; U; en) Presto/2.1.1,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:2873 Date: 2008-12-04T12:47:30-08:00 List-Id: 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.... ;-)