function pointer & Thread Parameter :: Multithreading

This is a discussion on function pointer & Thread Parameter :: Multithreading within the Windows Programming forums, part of the Platform Specific Boards category; Hi. Is it possible to pass a pointer to a class member function to a worker thread? Otherwise, is it ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    function pointer & Thread Parameter :: Multithreading

    Hi.

    Is it possible to pass a pointer to a class member function to a worker thread?

    Otherwise, is it possible to pass in a global function? If yes, do you have to use typedef (*func)(parameters)?

    Thanks,
    Kuphryn

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    13
    Do you mean the
    AfxBeginThread type of worker thread? or do you mean the _beginthread type of thread????

    All Thread Function for the AfxBeginThread must be decalared like this:
    Code:
    UINT ThreadProc(VOID * pVoid)
    However, you can name it whatever you want pass a pointer that is cast to your object and the function can be modifed to take a pointer to your object.

    Code:
    UINT MyThreadProc(MyOject * pMyObject);
    This is the call the to start the thread:

    Code:
    CWinThread * pThread;
    MyObject * p_MyObject;
    
    pThread = AfxBeginThread(
      (AFX_THREADPROC)MyThreadProc,
      (MyObject *)p_MyObject
    );
    Inside the MyThreadProc you can access that object's methods. A pointer to any method may be possible of you could figure out how to use the pointer at the other end.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay. Thanks.

    I believe you meant passing a function to CreateThread() or _beginThread(). I know that is possible. In fact, that is a necessity if you want to call a function to do work.

    I am interested more in passing in the thread a function pointer. In other words, I want to call a function while inside the thread.

    Kuphryn

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    DrPizza of GameDev brought up a solution I missed yesterday. The solution is to encapsulate a function in a struct that is a class member. That will allow you go not have to declare a global function.

    The only thing I am uncertain of is if you have a private member struct, will the global worker thread function i.e. the function you pass to __BeginThread() recognize the abstract private member.

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. cannot start a parameter declaration
    By Dark Nemesis in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2005, 03:09 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

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