Problem using member function when dealing with threading

This is a discussion on Problem using member function when dealing with threading within the C++ Programming forums, part of the General Programming Boards category; hi, i am new in programming and now dealing with some multi threading stuff anyway i got problem when trying ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    114

    Smile Problem using member function when dealing with threading

    hi, i am new in programming and now dealing with some multi threading stuff

    anyway i got problem when trying to use member function when doing threads

    i searched around the net and found that 1 method is by using messages which is rather complex

    another method that somebody posted is by casting:

    Code:
    DWORD pg_control::thr ( LPVOID lpvParam ) // static
    {
        pg_control *thr = (pg_control*)lpvParam;
        
        thr->get_hosts(0,NULL);
        // Go on with code ...
        return 0;
    }
    
    
    void pg_control::OnNn()
    {   
       AfxBeginThread (   (AFX_THREADPROC)pg_control::thr,  (LPVOID)this );}


    however i get some errors

    C:\Program Files\Microsoft Visual Studio\MyProjects\abc.cpp(222) : error C2440: 'type cast' : cannot convert from '' to 'unsigned int (__cdecl *)(void *)'
    None of the functions with this name in scope match the target type
    Error executing cl.exe.

    AClient.exe - 1 error(s), 0 warning(s)

    the problem lies in the red text
    can anyone please correct and enlighten me?
    thanks!

    - using winxp programming for win98se
    - MFC style
    - VC++6.0 SP6

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,434
    Thread functions are typically normal functions (eg "void *Function(void *)"), not non-static member functions of classes. If you really want the function to be a member of a class, make it static (i.e. it will not receive a hidden `this' pointer when it is called).

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    hi, i am new in programming and now dealing with some multi threading stuff
    anyway i got problem when trying to use member function when doing threads
    i searched around the net and found that 1 method is by using messages which is rather complex
    If you're using MFC, using asynchronous messages requires a bit of initial work, such as deriving from CWinThread, RegisterWindowMessage, GUID's, but it's more tedious than difficult. (Difficult is sorting out the bizarre behavior when you make threading mistakes.) You might even wrapping the PostThreadMessage calls, prossibly in a proxy class, but first decide how you want the method to behave. If you want to call the method of an active object, have the method processed in the other thread, and have the method return immediately to caller, then you're want to use asynchronous messages. Otherwise, if you only want to set some data in the active object, try use a regular messsage. The setting of the data won't be handled by the active object's thread but by the caller's thread.

    another method that somebody posted is by casting:
    I don't think casting the function pointer is such a good idea. Can that even be done? If you're using worker threads, you're better off sending the pointer to the object in the LPVOID parameter and then casting the LPVOID to the right type.

  4. #4
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    arghh.. please don't mix MFC with threading.. that's awfull... I'd prefer a million times to use CreateThread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 2
    Last Post: 04-19-2008, 12:06 AM
  3. Replies: 6
    Last Post: 04-27-2006, 10:55 AM
  4. problem returning struct member from function
    By yukster in forum C Programming
    Replies: 6
    Last Post: 08-24-2003, 01:21 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21