Correct use of _beginthreadex?

Windows specific questions.
Post Reply
Wilko
Posts: 23
Joined: Oct 26, 2016 7:57

Correct use of _beginthreadex?

Post by Wilko »

Hi,
I have been struggling with threads but got it working now. The only problem is two compiler warnings in one line...

The line of code is

Code: Select all

    hThr = _beginthreadex(NULL, 0, @HandleUpdates, NULL, 0, NULL)
HandleUpdates is a SUB, not a FUNCTION.

It gives these warnings:
warning 3(1): Passing different pointer types, at parameter 3 of _BEGINTHREADEX()
warning 4(1): Suspicious pointer assignment

The function declaration in process.bi is:

Code: Select all

declare function _beginthreadex (byval as any ptr, byval as ulong, byval as function stdcall(byval as any ptr) as ulong, 'byval as any ptr, byval as ulong, byval as ulong ptr) as uintptr_t
Any suggestions how to change the code to eliminate the compiler warnings?
Any help is appreciated, Wilko
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Correct use of _beginthreadex?

Post by St_W »

Wilko wrote:[...]HandleUpdates is a SUB, not a FUNCTION.[...]
I'm wondering why this doesn't cause troubles for you as the documentation says that stdcall is used, effectively meaning that your function should remove 4 bytes from the stack, which isn't done when you're using a SUB without a return value. I would expect some stack corruption or stack overflow resulting in weird runtime behaviour and/or a crash.

So the compiler warnings are completely valid. You need to adjust your callback method to match the function signature as described on MSDN.
Wilko
Posts: 23
Joined: Oct 26, 2016 7:57

Re: Correct use of _beginthreadex?

Post by Wilko »

Thanks. It's not that I doubt the warnings are correct; sure they are. I just do not know how to change my code to avoid the warnings. If I change the Sub into a Function, the warnings are the same. So I would appreciate tips how to change the code in order to avoid the warnings.
Thanks,
Wilko
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Correct use of _beginthreadex?

Post by St_W »

The following code should compile without warnings:

Code: Select all

#include "crt.bi"
#include "crt/stdint.bi"
#include "crt/win32/process.bi"

dim as uintptr_t hThr

function HandleUpdates stdcall (byval arg as any ptr) as ULong
	return 0
End Function

hThr = _beginthreadex(NULL, 0, @HandleUpdates, NULL, 0, NULL)
Please double check that your function signature matches the declaration in the process.bi header. This also applies to the "stdcall" and "byval" specifiers.

Are you using the latest compiler version and its headers? Older version shipped with headers that defined the return value as "UInteger".
Wilko
Posts: 23
Joined: Oct 26, 2016 7:57

Re: Correct use of _beginthreadex?

Post by Wilko »

Thanks a lot; now it compiles without warnings. And it still works ;-)
Wilko
Post Reply