Article View: comp.os.os2.programmer.misc
Article #209Re: _beginthread() data ptr question
From: Hawley K Rising
Date: Mon, 29 Aug 1994 22:49
Date: Mon, 29 Aug 1994 22:49
64 lines
2946 bytes
2946 bytes
Keith Bennett (kbennett@cpcug.org) wrote:
: This question concerns the 4th parameter passed to _beginthread().
: This is the void * that can point to anything the programmer wants
: it to.
: I have a function that loops and starts 'n' separate threads (pls see
: example below). In order to be safe, do I need to guarantee that the
: memory pointed to by that pointer I pass is active and dedicated
: exclusively to that thread for the thread's lifetime? (Assuming I
: don't *want* any interference from other threads.)
: For example, I assume that the code below will not work properly because
: the contents pointed to by &n may change before the newly started thread
: can read it:
: void _Optlink my_thread_func( void *args)
: {
: int the_number_passed = * (int *) args; /* ... */
: }
: void my_thread_starting_func()
: {
: for( int n = 0; n < 4; n++)
: _beginthread( my_thread_func, 0, 32 * 1024, &n);
: }
: But how about if I change this to:
: void my_thread_starting_func()
: {
: int n[4];
: n[0] = 0; n[1] = 1; n[2] = 2; n[3] = 3;
: for( int i = 0; i < 4; i++)
: _beginthread( my_thread_func, 0, 32 * 1024, &n[i]);
: }
: What if 'my_thread_starting_func()' finishes before one or more of my
: 'my_thread_func()''s reads the value, and that stack area is already
: overwritten by another function?
: So, am I correct in assuming that in this situation I must absolutely
: guarantee that this memory will be used exclusively by that thread
: for as long as the thread might need it? And therefore, that I should
: probably use a static int array?
I haven't tried it so I may have this all wrong, but Dorfman and
Neuberger (Effective Multithreading in OS/2) list a function called
_threadstore() which is to be used in conjunction with _beginthread and
_endthread for "per thread data area". the prototype is
#include <process.h>
void **_threadstore(void);
They are using the CSet++ compiler.
Hope this helps,
Hawley Rising
rising@crl.com
Technical Details
Message-ID:
<33tolb$2qq@crl2.crl.com>Path:
arc1-ca.pugleaf.net!bga.com!news.sprintlink.net!redstone.interpath.net!ddsw1!panix!MathWorks.Com!yeshua.marcam.com!charnel.ecst.csuchico.edu!olivea!decwrl!nntp.crl.com!crl2.crl.com!not-for-mailReferences:
<33tkbt$ssr@cpcug.org>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cd94,ce25cc05cfc65cdb X-Google-Attributes: gid10cd94,public X-Google-Thread: 11307a,ce25cc05cfc65cdb X-Google-Attributes: gid11307a,public X-Google-ArrivalTime: 1994-08-29 15:57:12 PST Path: arc1-ca.pugleaf.net!bga.com!news.sprintlink.net!redstone.interpath.net!ddsw1!panix!MathWorks.Com!yeshua.marcam.com!charnel.ecst.csuchico.edu!olivea!decwrl!nntp.crl.com!crl2.crl.com!not-for-mail From: rising@crl.com (Hawley K Rising) Newsgroups: comp.os.os2.programmer.misc,bit.listserv.os2-l Subject: Re: _beginthread() data ptr question Followup-To: comp.os.os2.programmer.misc,bit.listserv.os2-l Date: 29 Aug 1994 15:49:15 -0700 Organization: CRL Dialup Internet Access (415) 705-6060 [login: guest] Lines: 65 Message-ID: <33tolb$2qq@crl2.crl.com> References: <33tkbt$ssr@cpcug.org> NNTP-Posting-Host: crl2.crl.com X-Newsreader: TIN [version 1.2 PL2]