Click here to Skip to main content
15,747,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
uintptr_t _beginthreadex( // NATIVE CODE
   void *security,
   unsigned stack_size,
   unsigned ( __stdcall *start_address )( void * ),
   void *arglist,
   unsigned initflag,
   unsigned *thrdaddr

I want to start a thread from socket winsock

I want to create a thread from socket function

should my argument list be indirect variable to address of data item from my orginal thread? or can the arglist be any data item acessed with mdifying variable? or should arglist be from where i Began and started my thread offset base application from?

Should I use native or managed code for socket start address?

For /clr code, _beginthread and _beginthreadex each have two overloads. One takes a native calling-convention function pointer, and the other takes a __clrcall function pointer. The first overload isn't application domain-safe and never will be. If you're writing /clr code, you must ensure that the new thread enters the correct application domain before it accesses managed resources. You can do so, for example, by using call_in_appdomain. The second overload is application domain-safe; the newly created thread will always end up in the application domain of the caller of _beginthread or _beginthreadex.

By default, this function's global state is scoped to the application. To change this behavior, see Global state in the CRT.

I am concerned that native is not application domain safe in context of socket function thread after beginthread(). Should there be a function before beginthreadex()? should arguments passed to beginthreadex() be address to data item indirectly that alters the orginal context of the prior base thread?

arglist is a parameter to be passed to the newly created thread. Typically, it's the address of a data item, such as a character string. arglist can be NULL if it isn't needed, but _beginthread and _beginthreadex must be given some value to pass to the new thread. All threads are terminated if any thread calls abort, exit, _exit, or ExitProcess.

int thrsrt = accept(servsock, (SOCKADDR *)&servaddr,servlngth);

int thr = beginthreadex(NULL,0,thrsrt,thrdproc,NULL)

int thrdproc = accept(servsock, (SOCKADDR *)&servaddr,servlngth);
return 0;

void endthreadex()

What I have tried:

Ive tried original beginthread and compared with create thread if that helps explain the actual systematic structure Im looking for.
Updated 19-May-23 11:41am

1 solution

The use of _beginthreadex() is explained in detail here

_beginthread, _beginthreadex | Microsoft Learn

Here you can read that the function creates a thread which starts the execution of a routine at start_address.

If you set almost all parameters to NULL anyway, it might make more sense to use a variant where you don't have to specify these parameters in the first place. Only if at least one of the additional parameters is really needed, it would make sense.

The question whether you have to use the native or managed version results from your choice of programming language and would have to be told rather than asked by you.

Your proposed code section contains so many errors that one wonders if you even looked at the associated help page.

Besides the correct notation, with underscore, you obviously need a function that you can start as a thead. This function should call _endthread() at the end.
void MyThread(void* parg)
    // ...
You can then pass the address of this function, considering if and which parameters you want to pass to the thread.
int  param = 0;
int* pparam = &param;
_beginthread( MyThread, 0, (void*)pparam );
Share this answer
CPallini 18-May-23 2:26am    
5.German is a bit difficult, for me. :-)
merano99 18-May-23 12:17pm    
thanks for the tip, I do not notice such things :- ), and thanks for 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900