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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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,
                                    ( LPVOID) &dummy,

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?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
For MFC programs, use ::AfxBeginThread instead.
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  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, may be i can help.

Feel free to ask.
Vinayak KumbarSr Program ManagerCommented:

Why can't u try allocating memory to Urself. as
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
      delete myexa;
      myexa = NULL;

here example is defined as
typedef struct
      int i;
      int j;

Try it out.
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.