Article View: comp.os.os2.programmer.misc
Article #206Re: _beginthread() data ptr question
From: Kevin Vigor
Date: Tue, 30 Aug 1994 17:25
Date: Tue, 30 Aug 1994 17:25
54 lines
2114 bytes
2114 bytes
My usual strategy for passing data to threads is to use malloc'd
memory. It seems that in C Set++, at least, memory can be malloc'd by
one thread and later freed by another with no ill effects (whether this
is guaranteed behavior I have no idea; anyone?), i.e.
void foo(int magicNumber)
{
int *ip;
ip = malloc(sizeof(int)); // SHuold checkfor failure here.
*ip = magic_number;
_beginthread(myThread, NULL, 4096L, ip);
}
void myThread(void *intPtr)
{
int magicNumber = *((int *)(intPtr));
free(intPtr);
...
_endthread();
}
This way you never have to worry about another thread stomping on
your data. Just be sure that the child thread frees the memory when it
is done.
By the way, if all you want is to pass an integer, you can just
typecast the last parameter, like so:
void foo(int magicNumber)
{
_beginthread(myThread, NULL, 4096L, (void *)magicNumber);
}
void myThread(void *intPtr)
{
int magicNumber = (int)intPtr;
...
_endthread();
}
This is of course pretty evil, and relies on
sizeof(int) <= sizeof(void *), which you should at least put into an
assert if you use this particular hack.
--
Kevin
Technical Details
Message-ID:
<1994Aug30.172539.29219@wicat.com>Path:
arc1-ca.pugleaf.net!bga.com!news.sprintlink.net!redstone.interpath.net!ddsw1!panix!MathWorks.Com!yeshua.marcam.com!zip.eecs.umich.edu!newsxfer.itd.umich.edu!gatech!news.byu.edu!hamblin.math.byu.edu!wicat!elvis.wicat.com!kevinReferences:
<33tkbt$ssr@cpcug.org>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 11307a,ce25cc05cfc65cdb X-Google-Attributes: gid11307a,public X-Google-Thread: 10cd94,ce25cc05cfc65cdb X-Google-Attributes: gid10cd94,public X-Google-ArrivalTime: 1994-08-30 12:01:57 PST Newsgroups: comp.os.os2.programmer.misc,bit.listserv.os2-l Path: arc1-ca.pugleaf.net!bga.com!news.sprintlink.net!redstone.interpath.net!ddsw1!panix!MathWorks.Com!yeshua.marcam.com!zip.eecs.umich.edu!newsxfer.itd.umich.edu!gatech!news.byu.edu!hamblin.math.byu.edu!wicat!elvis.wicat.com!kevin From: kevin@elvis.wicat.com (Kevin Vigor) Subject: Re: _beginthread() data ptr question Message-ID: <1994Aug30.172539.29219@wicat.com> Followup-To: comp.os.os2.programmer.misc Sender: news@wicat.com Organization: WICAT Systems X-Newsreader: TIN [version 1.2 PL2] References: <33tkbt$ssr@cpcug.org> Date: Tue, 30 Aug 1994 17:25:39 GMT Lines: 54