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?
carlpaddickAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
abancroftConnect With a Mentor Commented:
Sounds like the first thread has a bug that is screwing up the contents of the structure.

What happens if you don't pass the structure to the first thread prior to creating the second thread?
0
 
jkrCommented:
How do you 'pass' the structure to the thread? There should be no problems when using a pointer to the struct, e.g.

    struct example dummy;

    // kick off thread
    hThread =   ::CreateThread  (   NULL,
                                    0,
                                    ::MyThread,
                                    ( LPVOID) &dummy,
                                    0,
                                    &dwTID
                                );

0
 
chensuCommented:
For MFC programs, use ::AfxBeginThread instead.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
abdijCommented:
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
0
 
Vinayak KumbarSr Program ManagerCommented:
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(MyCounterThread, 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
0
 
carlpaddickAuthor Commented:
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.

0
All Courses

From novice to tech pro — start learning today.