-
August 30th, 2011, 05:55 AM
#1
CreateThread and few arguments
Hey
I'm stuck with the following code. I need to pass a few variables address to new thread to got a shared access to them but the way I choose seems doesn't work. I got in new thread a good pointer to params class but bool and intereger variable addresses are not proper. Where can be the problem??
Thanks!
#include"threads.h"
class params
{
public:
bool* bl;
int* i;
HANDLE* h;
};
DWORD WINAPI ThreadProc(
__in LPVOID lpParameter
)
{
bool* p_b;
int* p_i;
HANDLE h;
params* ptr=(params*)lpParameter;
p_b=ptr->bl; //that's the first problem (often even 0 value for pointer)
p_i=ptr->i; //that's the second problem
h=(*ptr->h);
return 0;
}
void initThread()
{
bool a=true;
int b=1;
HANDLE h=CreateMutex(NULL,TRUE,L"TEST");
params prm;
prm.bl=&a;
prm.h=&h;
prm.i=&b;
CreateThread(0,0,ThreadProc,&prm,0,0);
}
-
August 30th, 2011, 07:21 AM
#2
Re: CreateThread and few arguments
All the variables whose addresses you're passing to the thread function as well as the params object itself are local to the function initThread(). They go out of scope and cease to exist once that function returns. Of course, initThread() does not wait for the newly created thread to finish before returning: That's what threads are made for!
Please use code tags when posting code.
Ah, and... Welcome to CodeGuru!
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
August 30th, 2011, 10:29 AM
#3
Re: CreateThread and few arguments
It's not just about the variables going out of scope, each thread has it's own stack and the params variable he creates is in the main thread. In order to access the variables in the newly created thread it needs to be on the heap or be a global variable.
-
August 30th, 2011, 12:52 PM
#4
Re: CreateThread and few arguments
Originally Posted by CppCoder2010
It's not just about the variables going out of scope, each thread has it's own stack and the params variable he creates is in the main thread. In order to access the variables in the newly created thread it needs to be on the heap or be a global variable.
Post #2 pointed out that because the mutex and thread handles are local to the function that created the thread and mutex, they will not be available after the function goes out of scope - so it really is a scoping issue.
Rather than coding in c-style, the OP may want to create a class instead. That way, the thread and mutex handles can be a class member, and the class destructor can ensure the thread is shutdown cleanly and clean up the thread and mutex handles.
Also, prefer using _beginthreadex over CreateThread.
-
August 31st, 2011, 04:18 AM
#5
Re: CreateThread and few arguments
Guys thanks for help, I totally forgot that this variables will be on local stack.....
Why you prefer _beginthreadex over CreateThread???
-
August 31st, 2011, 05:33 AM
#6
Re: CreateThread and few arguments
Best regards,
Igor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|