Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Threads in Classes

Threads in Classes

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mmica
    wrote on last edited by
    #1

    Hi all, When using the CreateThread within a class, is it possible to provide a method of the class as the lpStartAddress? m_hBroadcastThread = CreateThread(0,0, (LPTHREAD_START_ROUTINE)BroadcastThread, 0,0,&id); ... where BroadcastThread is the name of a method within the class. It's header is as follows: DWORD CNotificationsHandler::BroadcastThread(LPDWORD param) I keep getting the following compiler error: error C2440: 'type cast' : cannot convert from '' to 'unsigned long (__stdcall *)(void *)' Your help would be greatly appreciated. Mark

    P M B 3 Replies Last reply
    0
    • M mmica

      Hi all, When using the CreateThread within a class, is it possible to provide a method of the class as the lpStartAddress? m_hBroadcastThread = CreateThread(0,0, (LPTHREAD_START_ROUTINE)BroadcastThread, 0,0,&id); ... where BroadcastThread is the name of a method within the class. It's header is as follows: DWORD CNotificationsHandler::BroadcastThread(LPDWORD param) I keep getting the following compiler error: error C2440: 'type cast' : cannot convert from '' to 'unsigned long (__stdcall *)(void *)' Your help would be greatly appreciated. Mark

      P Offline
      P Offline
      Prakash Nadar
      wrote on last edited by
      #2

      If the thread method is in the class then it should be declared as static. The member functions of the class has the calling convention as thiscall too, which need to be removed by making it static. and while creating the thread pass 'this' pointer to the thread so that the thread function can access the member functions and variables of the class.


      MSN Messenger. prakashnadar@msn.com

      M 1 Reply Last reply
      0
      • P Prakash Nadar

        If the thread method is in the class then it should be declared as static. The member functions of the class has the calling convention as thiscall too, which need to be removed by making it static. and while creating the thread pass 'this' pointer to the thread so that the thread function can access the member functions and variables of the class.


        MSN Messenger. prakashnadar@msn.com

        M Offline
        M Offline
        mmica
        wrote on last edited by
        #3

        Worked wonderfuly. Thanks :-)

        1 Reply Last reply
        0
        • M mmica

          Hi all, When using the CreateThread within a class, is it possible to provide a method of the class as the lpStartAddress? m_hBroadcastThread = CreateThread(0,0, (LPTHREAD_START_ROUTINE)BroadcastThread, 0,0,&id); ... where BroadcastThread is the name of a method within the class. It's header is as follows: DWORD CNotificationsHandler::BroadcastThread(LPDWORD param) I keep getting the following compiler error: error C2440: 'type cast' : cannot convert from '' to 'unsigned long (__stdcall *)(void *)' Your help would be greatly appreciated. Mark

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          See the FAQ 6.1 Why can't I use a member function as a callback?[^] --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Pinky, are you pondering what I'm pondering? I think so Brain, but if we shaved our heads, we'd look like weasels!

          1 Reply Last reply
          0
          • M mmica

            Hi all, When using the CreateThread within a class, is it possible to provide a method of the class as the lpStartAddress? m_hBroadcastThread = CreateThread(0,0, (LPTHREAD_START_ROUTINE)BroadcastThread, 0,0,&id); ... where BroadcastThread is the name of a method within the class. It's header is as follows: DWORD CNotificationsHandler::BroadcastThread(LPDWORD param) I keep getting the following compiler error: error C2440: 'type cast' : cannot convert from '' to 'unsigned long (__stdcall *)(void *)' Your help would be greatly appreciated. Mark

            B Offline
            B Offline
            Blake Miller
            wrote on last edited by
            #5

            Also, if you are using the C Runtime Library AT ALL, you might want to be using beginthreadex instead of CreateThread. CreateThread does NOT initialize some thread local data used by each thread in the C run time library.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups