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. Passing parameters to threads

Passing parameters to threads

Scheduled Pinned Locked Moved C / C++ / MFC
helpcsharpc++question
3 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.
  • N Offline
    N Offline
    Nikhil Wason
    wrote on last edited by
    #1

    I am having a problem in passing an integer as an agrument to a thread of the format: UINT threadname (LPVOID); I am passing 4 distinct values to 4 distinct threads using the same function as the thread operation. Whenever i typecast the address of the interger to LPVOID type, pass it an argument to CreateThread() & then re-typecast it to an integer in the thread function, i either get runtime errors or i get junk values. but never the original value of the integer that i wanted. Is there any special way of typecasting pointers in vc++.net?? I also tried out using the reinterpret_cast operator and also replacing the CreateThread() function by AfxBeginThread(). Although i dont get any runtime errors now, but the same value is passed to 2 threads occasionaly (Rememeber i want 4 distinct values to be passed to 4 distinct threads) & sometimes a completely strange no is passed. The value is repeated for only 1 thread, the others are ok. Plz help if any1 knows a workaround or solution to this. -- Nikhil

    J A 2 Replies Last reply
    0
    • N Nikhil Wason

      I am having a problem in passing an integer as an agrument to a thread of the format: UINT threadname (LPVOID); I am passing 4 distinct values to 4 distinct threads using the same function as the thread operation. Whenever i typecast the address of the interger to LPVOID type, pass it an argument to CreateThread() & then re-typecast it to an integer in the thread function, i either get runtime errors or i get junk values. but never the original value of the integer that i wanted. Is there any special way of typecasting pointers in vc++.net?? I also tried out using the reinterpret_cast operator and also replacing the CreateThread() function by AfxBeginThread(). Although i dont get any runtime errors now, but the same value is passed to 2 threads occasionaly (Rememeber i want 4 distinct values to be passed to 4 distinct threads) & sometimes a completely strange no is passed. The value is repeated for only 1 thread, the others are ok. Plz help if any1 knows a workaround or solution to this. -- Nikhil

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      Whenever i typecast the address of the interger to LPVOID type... Here's the problem, I guess. You are passing the address of an int declared on the stack. As soon as you exit the function from where you're calling CreateThread, that int gets destroyed, so that when the thread tries to recover it it is likely garbage that's being retrieved instead. As an int and LPVOID have the same size (in Win32), you can just reinterpret_cast the int (not its address!) to a LPVOID, and you should be done. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]!

      1 Reply Last reply
      0
      • N Nikhil Wason

        I am having a problem in passing an integer as an agrument to a thread of the format: UINT threadname (LPVOID); I am passing 4 distinct values to 4 distinct threads using the same function as the thread operation. Whenever i typecast the address of the interger to LPVOID type, pass it an argument to CreateThread() & then re-typecast it to an integer in the thread function, i either get runtime errors or i get junk values. but never the original value of the integer that i wanted. Is there any special way of typecasting pointers in vc++.net?? I also tried out using the reinterpret_cast operator and also replacing the CreateThread() function by AfxBeginThread(). Although i dont get any runtime errors now, but the same value is passed to 2 threads occasionaly (Rememeber i want 4 distinct values to be passed to 4 distinct threads) & sometimes a completely strange no is passed. The value is repeated for only 1 thread, the others are ok. Plz help if any1 knows a workaround or solution to this. -- Nikhil

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        Yes it is bad problem... but try this ... instead pass iteager value as address pass a pointer to integer valeu... once i have similar problem .... i pass adress of varible with 3 distinct values to 3 ditinct threads but only first value is passed to all 3 distinct threads (no debug errors, no runtimes errors, no linking errors). Also in debbug mode it worked proparly but in release it didn't work proparly. The solution was to pass pointer to my varible instead as regular parameter. Some thing like that: int *p = new int; *p = 5; CreateThread(..., p, ...);

        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