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. How to transfer the local value into a thread??

How to transfer the local value into a thread??

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestionlearning
5 Posts 2 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.
  • R Offline
    R Offline
    roboo
    wrote on last edited by
    #1

    the language is ansi c language,not C++ I mean not to use the global resource include the file,reg and global variable I am I,Who is who,I am who,Who am I

    I 1 Reply Last reply
    0
    • R roboo

      the language is ansi c language,not C++ I mean not to use the global resource include the file,reg and global variable I am I,Who is who,I am who,Who am I

      I Offline
      I Offline
      ian mariano
      wrote on last edited by
      #2

      If this is Win32: Sharing Files and Memory[^]. It's worthwhile to note that memory allocated in a process can be shared between the threads of the process. So you can pass around parameters to your thread indicating where in memory to look for your data, but that's cumbersome, and prone to errors if you goof. You could just pass the variables as parameters into your thread entry points.

      "The greatest danger to humanity is humanity without an open mind."
        - Ian Mariano - http://www.ian-space.com/

      R 1 Reply Last reply
      0
      • I ian mariano

        If this is Win32: Sharing Files and Memory[^]. It's worthwhile to note that memory allocated in a process can be shared between the threads of the process. So you can pass around parameters to your thread indicating where in memory to look for your data, but that's cumbersome, and prone to errors if you goof. You could just pass the variables as parameters into your thread entry points.

        "The greatest danger to humanity is humanity without an open mind."
          - Ian Mariano - http://www.ian-space.com/

        R Offline
        R Offline
        roboo
        wrote on last edited by
        #3

        I know.But the parameter is the LPVOID data type.I had tried to change the type by using (int *),but I got a wrong value. Can you tell me how to complete it using parameter? I am I,Who is who,I am who,Who am I

        I 2 Replies Last reply
        0
        • R roboo

          I know.But the parameter is the LPVOID data type.I had tried to change the type by using (int *),but I got a wrong value. Can you tell me how to complete it using parameter? I am I,Who is who,I am who,Who am I

          I Offline
          I Offline
          ian mariano
          wrote on last edited by
          #4

          Are you using CreateThread, or _beginthread? Either way, you can pass the parameter by casting it to void*:

          /* entry is the name of your thread entry point
          param is a POINTER to the structure you're trying to pass,
          (if it's not a pointer, the below should read (void*)&param)
          */
          DWORD dwThreadId = 0; /* will be filled in */

          HANDLE hThread = CreateThread(NULL, 0, entry, (void*)param, 0, &dwThreadId);

          If you're using functions from the C library, you SHOULD use _beginthread, and use the multithreaded C library:

          unsigned long hThread = _beginthread(entry, 0, (void*)param);

          Your entry function should look like:

          DWORD entry(void* pParam)
          {
          /* do something with pParam and work in thread
          for example, say its a pointer to a structure named mystruct:

             mystruct\* pMine = (mystruct\*)pParam;
             printf("Test = %\\n", pMine->strTest);
          \*/
          

          return 0;
          }

          "The greatest danger to humanity is humanity without an open mind."
            - Ian Mariano - http://www.ian-space.com/

          1 Reply Last reply
          0
          • R roboo

            I know.But the parameter is the LPVOID data type.I had tried to change the type by using (int *),but I got a wrong value. Can you tell me how to complete it using parameter? I am I,Who is who,I am who,Who am I

            I Offline
            I Offline
            ian mariano
            wrote on last edited by
            #5

            You should also note that if your data "goes away" in the thread that creates the new thread, then you'll probably get an access violation. The new thread, if created with CreateThread, the thread, even if not running, won't go away until you call CloseHandle on the handle returned by the function call. Read the dox for the APIs carefully: CreateThread[^] _beginthread, _beginthreadex[^]

            "The greatest danger to humanity is humanity without an open mind."
              - Ian Mariano - http://www.ian-space.com/

            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