Article View: comp.os.os2.programmer.misc
Article #211Re: _beginthread() data ptr question
From: Asbjorn Pettersen
Date: Tue, 30 Aug 1994 06:18
Date: Tue, 30 Aug 1994 06:18
74 lines
2888 bytes
2888 bytes
In article <33tkbt$ssr@cpcug.org>, kbennett@cpcug.org (Keith Bennett) writes:
>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?
>
>Thanks for your help.
>- Keith
>--
Use static int table.
void my_thread_starting_func()
{
static 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]);
}
Or this method with passing variable n itself (i haven't tested it ).
_beginthread( my_thread_func, 0, 32 * 1024, (void *) n);
And in Thread:
int the_number_passed = (int ) args;
Technical Details
Message-ID:
<33uivq$boc@nms.telepost.no>Path:
arc1-ca.pugleaf.net!bga.com!news.sprintlink.net!redstone.interpath.net!ddsw1!news.kei.com!uhog.mit.edu!europa.eng.gtefsd.com!howland.reston.ans.net!pipex!sunic!trane.uninett.no!eunet.no!nuug!telepost.no!apeReferences:
<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 23:18:50 PST Path: arc1-ca.pugleaf.net!bga.com!news.sprintlink.net!redstone.interpath.net!ddsw1!news.kei.com!uhog.mit.edu!europa.eng.gtefsd.com!howland.reston.ans.net!pipex!sunic!trane.uninett.no!eunet.no!nuug!telepost.no!ape From: ape@atlas.spacetec.no (Asbjorn Pettersen) Newsgroups: comp.os.os2.programmer.misc,bit.listserv.os2-l Subject: Re: _beginthread() data ptr question Date: 30 Aug 1994 06:18:34 GMT Organization: Spacetec A.S Lines: 77 Message-ID: <33uivq$boc@nms.telepost.no> References: <33tkbt$ssr@cpcug.org> NNTP-Posting-Host: atlas.spacetec.no