Sign in to follow this  

Multithreading + casting + stl = ouch...!

Recommended Posts

Hello, I am trying to implement multithreading in my app so that I can load parts of my interface in and out of memory without the user being able to notice much difference in responsiveness and more importantly so that the program uses much less memory at any one time. The app is basically a linear user interface in which the user is taken, sequentially from one screen to the next, the idea is this... As the user moves from, say, screen 1 onto screen 2, the interface releases anything resources associated with screen 1 and starts to load any resources needed by screen 3. The interface class maintains a list of pointers to the screen objects in an STL vector which looks like this...
vector<Screen*> ScreenList; // Holds a list of all the screen object in the interface

Screen is a base class from which the various types of screens are derived, each Screen object implements a ReleaseScreen() and an Initialise() method. The Interface class implements to static methods which are supposed to be run by the new threads each time a screen needs to be released or initialised, they look like this...
// From Interface.h
// Private thread functions
static DWORD WINAPI ReleaseScreen(LPVOID pvoid);
static DWORD WINAPI InitialiseScreen(LPVOID pvoid);

// and from Interface.cpp....
// Static private thread functions
DWORD Interface::InitialiseScreen(LPVOID pvoid)
	return (DWORD)1;

DWORD Interface::ReleaseScreen(LPVOID pvoid)
	return (DWORD)1;

As you can see from above the two methods which will be run by the threads expect 'pointers-to-screens' as the input. In the method within the Interface which spawns the threads (and contains the problem) the code looks like this...
// Release the previous screen
DWORD dwThreadParam = (DWORD)this;
DWORD dwThreadID;
DWORD m_hThread = CreateThread (NULL, 0, ReleaseScreen, static_cast<void*>(, 0, &dwThreadID);

// Initialise thread left out for brevity...

// Get previous screen is a simple helper method which returns an int location
// of the previous screen within the vector

However I get this problem when I try to compile the code.... C:\Documents and Settings\Mark\StarChart\Testing\Hierachical Screen Test\Interface.cpp(239) : error C2440: 'initializing' : cannot convert from 'void *' to 'unsigned long' This conversion requires a reinterpret_cast, a C-style cast or function-style cast I also tried the compiler's suggestion of using a reinterpret_cast, but no joy... Any help would be greatly appreciated. This is my first trip into the wonderful world of threads and so far I don't like it much! Kind regards, Mark Coleman

Share this post

Link to post
Share on other sites
The problem is not your cast, it is the return value of CreateThread. CreateThread returns a HANDLE, which is just a typedef/#define for a void *. You're trying to stuff it into a DWORD, which is a typedef/#define for unsigned long. Hence the cannot convert from 'void *' to 'unsigned long' instead of the cannot convert from *whatever* to 'void *' you would have gotten had the cast been the problem.


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this