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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Passing parameter in worker thread

Passing parameter in worker thread

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
5 Posts 3 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.
  • L Offline
    L Offline
    learningvisualc
    wrote on last edited by
    #1

    Hi all, I have made a worker thread. I want to pass a integer as a parameter in my thread. I am not getting how to do it. Can anybody help me this. Thanks in advance

    _ 1 Reply Last reply
    0
    • L learningvisualc

      Hi all, I have made a worker thread. I want to pass a integer as a parameter in my thread. I am not getting how to do it. Can anybody help me this. Thanks in advance

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      A thread accepts a void pointer as its parameter. If you want to pass an integer, you first typecast it as a void pointer and then typecast it back to an integer inside the thread function.

      «_Superman_»
      I love work. It gives me something to do between weekends.

      Microsoft MVP (Visual C++)

      Polymorphism in C

      L 1 Reply Last reply
      0
      • _ _Superman_

        A thread accepts a void pointer as its parameter. If you want to pass an integer, you first typecast it as a void pointer and then typecast it back to an integer inside the thread function.

        «_Superman_»
        I love work. It gives me something to do between weekends.

        Microsoft MVP (Visual C++)

        Polymorphism in C

        L Offline
        L Offline
        learningvisualc
        wrote on last edited by
        #3

        can you please explain me using a example.

        L _ 2 Replies Last reply
        0
        • L learningvisualc

          can you please explain me using a example.

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          Thread creation may look like this -

          CreateThread(0, 0, Start, (LPVOID)1024, 0, 0);

          Here 1024 is the value passed as parameter. The thread function may look like this-

          DWORD WINAPI Start(LPVOID p)
          {
          int i = (int)p;
          return 0;
          }

          Here the passed in parameter will be available in the variable i.

          «_Superman_»
          I love work. It gives me something to do between weekends.

          Microsoft MVP (Visual C++)

          Polymorphism in C

          1 Reply Last reply
          0
          • L learningvisualc

            can you please explain me using a example.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Why don't you post the code you have written for your thread so that we can help you better?

            ...byte till it megahertz... my donation to web rubbish

            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