Not logged in, Join Here! or Log In Below:  
News Articles Search    

 Home / General Programming / Multithreading + casting + STL = My head hurts... Account Manager
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.

May 24, 2005, 05:51 PM

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...

  2. 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...

  2. // From Interface.h
  3. // Private thread functions
  4. static DWORD WINAPI ReleaseScreen(LPVOID pvoid);
  5. static DWORD WINAPI InitialiseScreen(LPVOID pvoid);
  7. // and from Interface.cpp....
  8. // Static private thread functions
  9. DWORD Interface::InitialiseScreen(LPVOID pvoid)
  10. {
  11.         (static_cast<Screen*>(pvoid))->Initialise();
  12.         return (DWORD)1;
  13. }
  15. DWORD Interface::ReleaseScreen(LPVOID pvoid)
  16. {
  17.         (static_cast<Screen*>(pvoid))->ReleaseScreen();
  18.         return (DWORD)1;
  19. }

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...

  2. // Release the previous screen
  3. DWORD dwThreadParam = (DWORD)this;
  4. DWORD dwThreadID;
  5. DWORD m_hThread = CreateThread (NULL, 0, ReleaseScreen, static_cast<void*>(, 0, &dwThreadID);
  7. // Initialise thread left out for brevity...
  9. // Get previous screen is a simple helper method which returns an int location
  10. // of the previous screen within the vector

However I get this problem when I try to compile the code....

C:Documents and SettingsMarkStarChartTestingHierachical Screen TestInterface.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


May 24, 2005, 06:45 PM

Arrrgh!! Wrong return type... My bad!

Mark Coleman

This thread contains 2 messages.
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.