Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DWORD WINAPI mHandler(LPVOID sId) {
- ...
- arr[(int)sId]
- ...
- }
- int id=1;
- CreateThread(NULL, NULL, mHandler, (LPVOID)id, NULL, NULL);
- warning C4311: 'type cast' : pointer truncation from 'LPVOID' to 'int'
- warning C4312: 'type cast' : conversion from 'int' to 'LPVOID' of greater size
- // parameter on the heap to avoid possible threading bugs
- int* id = new int(1);
- CreateThread(NULL, NULL, mHandler, id, NULL, NULL);
- DWORD WINAPI mHandler(LPVOID sId) {
- // make a copy of the parameter for convenience
- int id = *static_cast<int*>(sId);
- delete sId;
- // now do something with id
- }
- DWORD WINAPI mHandler(LPVOID p)
- {
- INT_PTR id=reinterpret_cast<INT_PTR>(p);
- }
- ...
- INT_PTR id = 123;
- CreateThread(NULL, NULL, mHandler, reinterpret_cast<LPVOID>(id), NULL, NULL);
- static_assert(sizeof(int) <= sizeof(LPVOID),
- "Use new int() - cannot fit int to LPVOID");
- int id = 7;
- LPVOID idP = 0;
- memcpy(&idP, &id, sizeof(id));
- CreateThread(NULL, NULL, mHandler, idP, NULL, NULL);
- void mHandle(..., LPVOID idP, ...)
- {
- static_assert(sizeof(int) <= sizeof(LPVOID),
- "Use new int() - cannot fit int to LPVOID");
- int id;
- memcpy(&id, &idP, sizeof(id));
- }
Add Comment
Please, Sign In to add comment