Dave's Brain

Browse - Programming Tips - How do I pass an integer to a thread? Casting to/from LPVOID gives these errors:

Date: 2015apr20
Platform: win32

Q.  How do I pass an integer to a thread?  Casting to/from LPVOID gives these errors:
warning C4311: 'type cast' : pointer truncation from 'LPVOID' to 'int'
warning C4312: 'type cast' : conversion from 'int' to 'LPVOID' of greater size

A.  Add in an extra cast to INT_PTR like this:

void _cdecl MyThread(LPVOID pParam) { int myNumber = (int)(INT_PTR) pParam; //... } // Start the thread this way _beginthread(MyThread, 0, (LPVOID)(INT_PTR) myNumber);

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.