Passing parameters to threads
-
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
-
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
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 callingCreateThread
, thatint
gets destroyed, so that when the thread tries to recover it it is likely garbage that's being retrieved instead. As anint
andLPVOID
have the same size (in Win32), you can justreinterpret_cast
theint
(not its address!) to aLPVOID
, 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[^]! -
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
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, ...);