Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CreateThread problem... 1

Status
Not open for further replies.

mattKnight

Programmer
May 10, 2002
6,225
GB
Hi all,

First post in this forum, so pls forgive a newbie...
Background
OS Winnt sp6
Dev Platfom MSVC++ v6 SP5

I recently started "playing" with multi-threading (oh joy)
however I have a problem with CreateThread...
Code:
void CScheduler::Watcher()
{
int x = 5;  // arbitary parameter fr new thread
// Start new thread hThread and dwThreadID2 declared as
// class member variables

hThread = CreateThread(NULL,0,
          (LPTHREAD_START_ROUTINE)ThreadStart,
           (LPVOID)x, 0, &dwThreadID2);
// To do
// useful work and cleanup code (using thread 1)
}
DWORD WINAPI CScheduler::ThreadStart(LPVOID Arg)
{
// to do 
// some other useful work
//clean up code
}

This code is inside simple ATL object (set I think for free thread)
however, It doesn't compile - I get error C2440 at the CreateThread line
the error text reads
Code:
'type cast': cannot convert from '' to 'unsigned long )__stdcall *)(void *)'

None of the functions with this name in scope match the target type

I think that this is a scope issue with "ThreadStart", but where should it be (class, global etc)?

All help will be gratefully received

Thanks

Matt



 
Matt,

If you want to pass a pointer to a member function of a class, the member function must be declared as 'static' (the problem is that there is a hidden 'this' pointer for every non-static member function).

Within the header file declare

public:

static DWORD WINAPI CScheduler::ThreadStart (LPVOID lparam);

and within the code


hThread = CreateThread(NULL,0, CScheduler::ThreadStart,
(LPVOID)x, 0, &dwThreadID2);


Hope this helps.

Temps
 
Temps,

That's it! problem solved

There will be other problems, I am sure!
Thanks again

Matt
 
Coupla other quick questions...

should "threadStart" be private or public?
nothing other than private makes much sense to me!
and
What sort of cast should be used other than type cast (old style?)
i.e. should I replace
Code:
(LPTHREAD_START_ROUTINE)CScheduler::ThreadStart
with
Code:
reinterpret_cast<THREAD_START_ROUTINE>(CScheduler::Threadstart)
or
static_cast<> etc?

My understanding would be reinterpret_cast as I am trying to cast a funtion pointer void(?) to a typed function pointer.

Is that correct?

Matt
 
private makes sense.

You don't need to cast - the 'signature' of the static member function already matches what CreateThread is expecting.

Temps

 
Just a suggestion: I would use _beginthreadx rather than CreateThread as CreateThread can lead to sync problems if you are calling any standard c lib rtl functions.

Thanks,
John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top