Active Forums => Programs => Topic started by: SpriggsySpriggs on May 08, 2021, 09:31:48 pm
Title: WinAPI Threading (64 Bit)
Post by: SpriggsySpriggs on May 08, 2021, 09:31:48 pm
justsomeguy (https://www.qb64.org/forum/index.php?action=profile;u=821) had started a post (https://www.qb64.org/forum/index.php?topic=3865.0) in the Discussion section of the forum concerning threading in QB64 and I had replied with some code for Windows 64 bit threading using Win32 API code. I thought it deserved its own post since this is quite a feat when using Win32 API functions. The below code is based on the MSDN page on threading here (https://docs.microsoft.com/en-us/windows/win32/procthread/creating-threads). The code uses plenty of Win32 API functions so I hope it can be helpful to others who are venturing into external libraries and the WinAPI. It includes memory allocation functions, thread safe functions, function addresses, and even provides an example of using a QB64 function as a callback and as an exported function. The code I have provided is currently set up to run 20 threads. One can adjust the MAX_THREADS constant to see a different output.
The C++ header that you will need. Save it as threadwin.h
FormatMessage FORMAT_MESSAGE_ALLOCATE_BUFFER Or FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS,0, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),_Offset(lpMsgBuf),0,0