DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Use a pointer to a function to start a thread ???

  1. #1
    Join Date
    May 2005
    Posts
    2

    Use a pointer to a function to start a thread ???

    Hello, I've got this situtation:

    Assume there is a class Foo in a file Foo.cpp as such:

    #include <windows.h>
    #include <process.h>

    class Foo
    {
    Foo::Foo() {} //empty constructor
    Foo::~Foo() {} //empty destructor

    //this is a standard thread definition - at least for VStudio 6 C++
    void MainThread(PVOID void)
    {

    //do something
    while (some_boolean_variable == true)
    {
    //execute things inside the thread
    }
    }
    };


    OK so there it is a class Foo... Now assume there is a main program like this

    #include <windows.h>
    #include <process.h>

    ...blah...
    ...blah...

    Foo* f;

    ...blah...
    ...blah...

    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    ...blah...
    ...blah...
    WM_CREATE:
    f = new Foo();
    _beginthread(f->MainThread, 0, NULL);
    ...blah...
    ...blah...
    }

    ...blah...

    ..the end of the main file


    What I am trying to do is declare a pointer to class Foo and then use the MainThread function to begin a thread. VStudio complains with this message:

    _beginthread : cannot convert parameter 1 from 'void (void *)' to 'void (__cdecl *)(void *)

    The thing that VStudio doesn't like is f->MainThread. I have managed to make a thread work with a function that resides in the main file but I don't want that. (To make matters worse I will spawn more threads from within class Foo later on, which will reside in other files say Foo1, Foo2, ...)

    I don't know what to do! I tried adding a __cdecl statement in my Foo class, then add a void* ... Nothing happens! I simply don't know what to do!

    To cut a long story short as you can see I simply want to start a thread through my main program, but I want my thread to actually reside to another file, not the main file.

    Any ideas? :(

    Thanks!

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    We had a similar thread here a few weeks ago. Anyway, the bottom line is that you can't treat a nonstatic member function of class as a freestanding function. There are several workarounds, the most common of which is declaring the member function static. More techniques are explained here
    Danny Kalev

  3. #3
    Join Date
    May 2005
    Posts
    2

    Thanks... I found a workaround

    Thanks but I think I found a workaround solution.

    Anyways right after I posted the problem I realised that this is not the way to do it. I could make it a lot simpler by using a function for example like f->BeginThread() and f->EndThread() and simply put a while-true loop somewhere in my Foo class to act as a thread.

    Anyhow, I have Charles Petzold's book here for WIN32 API for Win98 and there is a chapter in there about multithreading and multitasking. I am going to make my Foo class listen to messages and then use SetEvent() and ResetEvent() to spawn other threads...

    Fingers crossed

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center