Article View: comp.os.os2.programmer.misc
Article #208_beginthread() data ptr question
From: Keith Bennett
Date: Mon, 29 Aug 1994 21:35
Date: Mon, 29 Aug 1994 21:35
56 lines
2672 bytes
2672 bytes
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
--
--------------------------------------------------------------------------
Keith Bennett Bennett Business Solutions, Inc.
C++/C Software Development 1852 Middlebridge Drive
kbennett@cpcug.org Silver Spring, MD USA 20906
Technical Details
Message-ID:
<33tkbt$ssr@cpcug.org>Path:
arc1-ca.pugleaf.net!bga.com!news.sprintlink.net!hookup!swrinde!howland.reston.ans.net!math.ohio-state.edu!jussieu.fr!centre.univ-orleans.fr!univ-lyon1.fr!swidir.switch.ch!newsfeed.ACO.net!Austria.EU.net!EU.net!uunet!news1.digex.net!digex.net!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cd94,ce25cc05cfc65cdb,start X-Google-Attributes: gid10cd94,public X-Google-Thread: 11307a,ce25cc05cfc65cdb,start X-Google-Attributes: gid11307a,public X-Google-ArrivalTime: 1994-08-29 15:02:50 PST Path: arc1-ca.pugleaf.net!bga.com!news.sprintlink.net!hookup!swrinde!howland.reston.ans.net!math.ohio-state.edu!jussieu.fr!centre.univ-orleans.fr!univ-lyon1.fr!swidir.switch.ch!newsfeed.ACO.net!Austria.EU.net!EU.net!uunet!news1.digex.net!digex.net!not-for-mail From: kbennett@cpcug.org (Keith Bennett) Newsgroups: comp.os.os2.programmer.misc,bit.listserv.os2-l Subject: _beginthread() data ptr question Date: 29 Aug 1994 17:35:57 -0400 Organization: Express Access Online Communications USA: 800-969-9090 Lines: 56 Message-ID: <33tkbt$ssr@cpcug.org> NNTP-Posting-Host: cpcug.org