2

I'm trying to port some code to 64-bit, but it seems that the thread address identifier in _beginthreadex is unsigned int which is 32-bits and I can't pass/receive a 64-bit address identifier from the function:

uintptr_t _beginthreadex( // NATIVE CODE
   void *security,
   unsigned stack_size,
   unsigned ( __stdcall *start_address )( void * ),
   void *arglist,
   unsigned initflag,
   unsigned *thrdaddr // <-- 32-bit address
);

I checked the MSDN documentation, but I didn't see a 64-bit version of the function. Am I including the wrong header, per-processor flag or is there some other way to create a thread with a 64-bit address identifier?

Update

The documentation states that the thrdaddr parameter is 32-bit:

Thrdaddr

Points to a 32-bit variable that receives the thread identifier. Might be NULL, in which case it is not used.
Kiril
  • 39,672
  • 31
  • 167
  • 226

3 Answers3

5

The thrdaddr parameter receives the thread ID. It is not the address of the thread function. It appears to be an exceedingly badly named parameter.

The start_address parameter is the thread function pointer and you can pass your 64 bit function pointer in that parameter.


Your update to the question suggests that you believe that the thread ID is a 64 bit value on 64 bit Windows. That is a mis-think. Thread IDs are 32 bit values on all flavours of Windows.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I thought that `thrdaddr` means "thread address", but I guess it means "thread identifier". In any case, the documentation still states that the identifier is a 32-bit value. – Kiril Feb 08 '12 at 17:29
  • 1
    Thread IDs are indeed 32 bit values. The documentation is correct and the parameter is correctly typed. It is just misleadingly named. – David Heffernan Feb 08 '12 at 17:30
  • OK, got it... I didn't realize that it's not possible to have a 64-bit value for the Thread ID. Thanks! – Kiril Feb 08 '12 at 17:35
1

From the documentation:

Thrdaddr

Points to a 32-bit variable that receives the thread identifier. Might be NULL, in which case it is not used.

In other words, thrdaddr gets the thread id. It is not an address for the thread.

In 64-bit, all pointers are 64-bits. So this just works.

Community
  • 1
  • 1
MSN
  • 53,214
  • 7
  • 75
  • 105
1

You're getting some of the arguments mixed up. The address for your thread proc will be passed in as start_address. thrdaddr is an optional parameter than receives the thread ID.

HANDLE hThread;
unsigned threadID;

hThread = (HANDLE)_beginthreadex(
    NULL,
    0,
    &YourThreadProc, // this is your thread procedure
    NULL,
    0,
    &threadID); // threadID will hold the ID of the new thread
Derek Park
  • 45,824
  • 15
  • 58
  • 76
  • I was (miss)reading `thrdaddr` as "thread address", however, I'm aware that it's not the thread address but the identifier. – Kiril Feb 08 '12 at 17:30
  • It is a very poor name. The function will work in 64-bit code just fine, though. – Derek Park Feb 08 '12 at 17:33