carlpaddick
asked on
Passing parameters in a CreateThread API call
Happy New Year to one and All.
Guys I am having a few problems with the ::CreateThread API function.
Basically, I wish to pass the address of a structure in the fourth parameter of CreateThread. This structure contains 2 pointers to 2 different classes and a variable of SOCKET type, ie
struct example
{
class1* m_pclass1;
class2* m_pclass2;
SOCKET m_sockexample;
}
The class pointers always contain valid pointers to data when CreateThread is called.
I also make sure to "detach" the socket member before it is passed to the CreateThread function.
However...when my structure arrives in the thread function that does the processing, one of the class pointers is NULL and the SOCKET does not contain the same number (if you run in debug mode) as it did in the calling thread. Socket communication then becomes impossible, because I am not talking on the orignal thread that was accepted.
An important thing worth mentioning, is that my structure has already been passed to one thread before it is passed to the final thread - so it is going across 2 threads. The class pointers within the structure seem to be fine when passed to the first thread, but in the second thread, one is strangely NULL'ed. I hope this all makes sense and you can help me. It's been baffling me for quite a while. Am I doing anything wrong?
Guys I am having a few problems with the ::CreateThread API function.
Basically, I wish to pass the address of a structure in the fourth parameter of CreateThread. This structure contains 2 pointers to 2 different classes and a variable of SOCKET type, ie
struct example
{
class1* m_pclass1;
class2* m_pclass2;
SOCKET m_sockexample;
}
The class pointers always contain valid pointers to data when CreateThread is called.
I also make sure to "detach" the socket member before it is passed to the CreateThread function.
However...when my structure arrives in the thread function that does the processing, one of the class pointers is NULL and the SOCKET does not contain the same number (if you run in debug mode) as it did in the calling thread. Socket communication then becomes impossible, because I am not talking on the orignal thread that was accepted.
An important thing worth mentioning, is that my structure has already been passed to one thread before it is passed to the final thread - so it is going across 2 threads. The class pointers within the structure seem to be fine when passed to the first thread, but in the second thread, one is strangely NULL'ed. I hope this all makes sense and you can help me. It's been baffling me for quite a while. Am I doing anything wrong?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
For MFC programs, use ::AfxBeginThread instead.
Hi,
I agree with abancroft. After the CreateThread() the calling thread is modifying the contents of the structure.
Also you have not mentioned the synchronization mechanism used. Its better you protect your structure by using semaphores or mutexes.
Also check if your firt thread is modifying (re-initializing) the contents of the structure anywhere later. Put a break point there and put a brek point in th begining of the thread. F5 and see which point is reached first. You are most likely to get the answer. Or post the code to abdij_b@hotmail.com, may be i can help.
Bye,
Feel free to ask.
Abdij
I agree with abancroft. After the CreateThread() the calling thread is modifying the contents of the structure.
Also you have not mentioned the synchronization mechanism used. Its better you protect your structure by using semaphores or mutexes.
Also check if your firt thread is modifying (re-initializing) the contents of the structure anywhere later. Put a break point there and put a brek point in th begining of the thread. F5 and see which point is reached first. You are most likely to get the answer. Or post the code to abdij_b@hotmail.com, may be i can help.
Bye,
Feel free to ask.
Abdij
Hi,
Why can't u try allocating memory to Urself. as
LPVOID s;
example *exa = new example;
exa->i = 10;
exa->j = 20;
s = (LPVOID)exa;
AfxBeginThread(MyCounterTh read, s);
where example is structure that contains those data
Then in the thread
example *myexa = (example *)(s);
//Process the data
if(myexa)
{
delete myexa;
myexa = NULL;
}
here example is defined as
typedef struct
{
int i;
int j;
}example;
Try it out.
VinExpert
Why can't u try allocating memory to Urself. as
LPVOID s;
example *exa = new example;
exa->i = 10;
exa->j = 20;
s = (LPVOID)exa;
AfxBeginThread(MyCounterTh
where example is structure that contains those data
Then in the thread
example *myexa = (example *)(s);
//Process the data
if(myexa)
{
delete myexa;
myexa = NULL;
}
here example is defined as
typedef struct
{
int i;
int j;
}example;
Try it out.
VinExpert
ASKER
aboncroft - you were right, functions called from the within the primary thread WERE changing the pointer data of one of the classes passed to the secondary thread.
Also, my thread function definitions were wrong. They were declared like this :
DWORD threadfunc(strucvar* pstruc)
when they should have been delcared as
DWORD threadfunc(LPDWORD p)
and then casting back to a pointer to my structure in the thread function, ie
DWORD threadfunc(LPDWORD p)
{
strucvar* sv;
sv = (strucvar *) p;
....
}
Thanks for your help, and ALL your replies. All the very best for 2000.
Also, my thread function definitions were wrong. They were declared like this :
DWORD threadfunc(strucvar* pstruc)
when they should have been delcared as
DWORD threadfunc(LPDWORD p)
and then casting back to a pointer to my structure in the thread function, ie
DWORD threadfunc(LPDWORD p)
{
strucvar* sv;
sv = (strucvar *) p;
....
}
Thanks for your help, and ALL your replies. All the very best for 2000.
struct example dummy;
// kick off thread
hThread = ::CreateThread ( NULL,
0,
::MyThread,
( LPVOID) &dummy,
0,
&dwTID
);