Is there a way to pass multiple parameters to the thread, instead of using global values?
Thank you.
Printable View
Is there a way to pass multiple parameters to the thread, instead of using global values?
Thank you.
Create a struct containing all the information you want to pass.
Create an instance of that struct(*), fill it with information, and then pass a pointer to the whole thing to the thread.
(*) you have to make sure that the scope of the memory allocated lasts at least as long as the thread being created. For example, pointers to local variables almost never work.
Can you please give me an example...Quote:
(*) you have to make sure that the scope of the memory allocated lasts at least as long as the thread being created. For example, pointers to local variables almost never work.
Yeah, use malloc
Ok, but how can I be sure they don't get mixed between different threads?
Because each thing returned by malloc is guaranteed to be a unique object which will last until you explicitly call free.
use different mallocs for different threads...Quote:
Originally Posted by Devil Panther
PS. And as I remember it is not recomended to call the CreateThread directly
use beginthreadex - it wraps the windows call and provides some additional initialization needed for correct work of the C/C++ runtime libraries
Here's what MSDN says about the issue.
Quote:
A thread in an executable that calls the C run-time library (CRT) should use the _beginthreadex and _endthreadex functions for thread management rather than CreateThread and ExitThread; this requires the use of the multi-threaded version of the CRT. If a thread created using CreateThread calls the CRT, the CRT may terminate the process in low-memory conditions.
I'm not sure this will work for me.Quote:
Originally Posted by Salem
Here is what I'm trying to do:
I'm scanning a directory for it's files.
Once the file is found I would like to open a new thread to process it.
How can I use malloc if the name of the variable is the same?
Where should I allocate the memory, inside the new thread?
thanks.
Something like that maybe:
Code:while(...)
{
int* a = malloc(sizeof *a);
long h;
if(a)
{
unsigned thread_id;
*a = rand(); //fill with something useful
h = _beginthreadex(NULL, 0, thread_proc, (void*)a, 0, &thread_id);
if(h != 0)
CloseHandle((HANDLE)h); //we not interested in the return value of the thread
}
}
unsigned __stdcall thread_proc( void * param)
{
int value = 0;
if(param)
value = *(int*)param;
//do processing
free(param);
return 0;
}
Oh... Using the address of the newly allocated variable.
Got it, thank you.
[strike]vart's example may kill the thread before it gets a chance to run (and free memory).[/strike](See below) (beginthread also returns uintptr_t.)
Also, pthreads are your friend.
That makes the example worth it...Quote:
*a = rand(); //fill with something useful
Yeah? And how exactly?Quote:
vart's example may kill the thread
MSDN says:
Quote:
Closing a thread handle does not terminate the associated thread.
/me retracts he previous statement as steathily as possible.
That seems illogical, but MSDN does say it... my mistake. (CBoard needs strikeout.)
Now you see why pthreads are my friend. :) All this nonsense between beginthread, beginthreadex, and family is driving me nuts.
It is interesting (at least for me) to note that you can easily pass this as the parameter, and connect the thread back to an object.
Oh yeah, MSDN says this about CloseHandle and threads.Code:m_hThread = ::CreateThread( 0, 0,
/* ( DWORD ( WINAPI * )( LPVOID ) ) */ run,
LPVOID(this), 0, &m_dwThreadID );
}
/* static */ DWORD WINAPI foo::run( LPVOID vthis )
{
foo * fthis = reinterpret_cast< foo * >( vthis );
return fthis->run();
}
DWORD foo::run( void )
{
// Do yr stuff with foo
}
>> Closing a thread handle does not terminate the associated thread. To remove a thread object, you must terminate the thread, then close all handles to the thread.