🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.os.os2.programmer.misc
4 messages
4 total messages Started by Keith Bennett Mon, 29 Aug 1994 21:35
_beginthread() data ptr question
#208
Author: Keith Bennett
Date: Mon, 29 Aug 1994 21:35
56 lines
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
Re: _beginthread() data ptr question
#209
Author: Hawley K Rising
Date: Mon, 29 Aug 1994 22:49
64 lines
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
Re: _beginthread() data ptr question
#210
Author: rmahoney on BIX
Date: Tue, 30 Aug 1994 00:05
7 lines
963 bytes
 
Protect the memory you pass to the thread with a semaphore.  The first
thing you do with the thread is copy the structure onto the stack and
then release the semaphore.  Or use the _threadstore call in CSEt++.
 
Bob
Re: _beginthread() data ptr question
#211
Author: Asbjorn Pettersen
Date: Tue, 30 Aug 1994 06:18
74 lines
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;
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads