   Multithreading questions
  Hello, I'm experimenting a bit with multithreading as eventually I would like to make a multithreaded server, but I've run into a few problems...First of all, I want to share the CLIENT struct with the thread, but at the same time I would like to store the threadhandle and ID in that shared struct. Is this possible at all? I have done the following and the thread doesn't seem to work...// Some info: I declare an array of CLIENT (clnt) to hold client connections.// In the createthread function below I use a member of that array as shared memory.struct CLIENT{    /* General socketstuff */    int connected;    SOCKET clientsocket;    SOCKADDR_IN c_Addr;    FD_SET fRead;    FD_SET fWrite;    /* Buffers etc... */    std::string rcvBuffer;    std::string sndBuffer;    /* Thread identifier and handle */    DWORD dwThreadID;    HANDLE hThreadHandle;    /* Info for thread */    int iThreadInfo;};/* A couple hundred lines below...... */if ( ( clnt.hThreadHandle = CreateThread ( NULL, NULL, (LPTHREAD_START_ROUTINE)&vSocketThread, &clnt, NULL, &clnt.dwThreadID ) ) == NULL ) {    std::cout << "error with thread" << std::endl;}And my last question... In the threadfunction itself I use cout at the beginning to check if the thread runs at all. I do not get to see any text in the console window so I assume the thread doesn't work. However, I realize that cout is not threadsafe, so I wonder, how to make it threadsafe, or is there another (threadsafe) way of printing text to the console window?   
  No answers at all? Or should I move this topic to another forum-category?The thing is, I can't really continue with what I'm working on until I get the multithreading working :(   ;  "GetCurrentProcessId" i advice that you check the msdn , you will find it extremly useful.   ;Quote:Original post by rogierpenninkFirst of all, I want to share the CLIENT struct with the thread, but at the same time I would like to store the threadhandle and ID in that shared struct. Is this possible at all? I have done the following and the thread doesn't seem to work...*** Source Snippet Removed ***Define "doesn't seem to work". Do you get a Compiler error? Linker error? Runtime error? Unexpected behaviour?;  Well, "doesn't seem to work" is exactly all that can be said :P There are no errors whatsoever, but even such simple things as putting some text in a console aren't executed by the thread...   ;  Firstly, try replacing you call to "CreateThread" with one to _beginthreadex. This is a wrapper around CreateThread which does some initialisation of the C runtime library structures.If this doesn't fix the problem, post some more code.   ;  Well, I am now getting one error by using _beginthreadex, but this is more because of my inability to implement it than anything else... I'll post the error, the start routine and the call to _beginthreadex itself ;)EDIT: I forgot to cast param 3 to (LPTHREAD_START_ROUTINE) but a new error popped...error C2664: '_beginthreadex' : cannot convert parameter 3 from 'unsigned long (__stdcall *)(void *)' to 'unsigned int (__stdcall *)(void *)'/* The CLIENT struct, this will also be the shared datastructure of multithreaded servers */struct CLIENT{/* General socketstuff */int connected;SOCKET clientsocket;SOCKADDR_IN c_Addr;FD_SET fRead;FD_SET fWrite;/* Buffers etc... */std::string rcvBuffer;std::string sndBuffer;/* Thread identifier and handle */DWORD dwThreadID;HANDLE hThreadHandle;/* Info for thread */int iThreadInfo;};// The "vSocketThread()" function. This function will be the function that is used by a multithreaded server to // start threads with. It consists of a loop that checks for data receival and data sending.unsigned __stdcall vSocketThread ( CLIENT *clnt ){std::cout << "are we in the running?" << std::endl;clnt->sndBuffer = "Some Information upon joining...";send ( clnt->clientsocket, clnt->sndBuffer.c_str(), strlen(clnt->sndBuffer.c_str()), 0 );// Loop until threadinfo is set to THREAD_EXIT;while ( clnt->iThreadInfo != THREAD_EXIT ) {FD_ZERO ( &clnt->fRead );FD_ZERO ( &clnt->fWrite );FD_SET ( clnt->clientsocket, &clnt->fRead );FD_SET ( clnt->clientsocket, &clnt->fWrite );int activity = select ( 0, &clnt->fRead, &clnt->fWrite, NULL, NULL );if ( activity == SOCKET_ERROR ) {closesocket ( clnt->clientsocket );clnt->connected = 0;clnt->hThreadHandle = NULL;clnt->dwThreadID = NULL;clnt->rcvBuffer = "";clnt->sndBuffer = "";break;}if ( FD_ISSET ( clnt->clientsocket, &clnt->fRead ) ) {if ( recv ( clnt->clientsocket, (char *)clnt->rcvBuffer.c_str(), BUFFERSIZE, 0 ) == SOCKET_ERROR ) {closesocket ( clnt->clientsocket );    clnt->connected = 0;    clnt->hThreadHandle = NULL;    clnt->dwThreadID = NULL;    clnt->rcvBuffer = "";    clnt->sndBuffer = "";    break;}}if ( clnt->sndBuffer.length() > 0 ) {if ( send ( clnt->clientsocket, clnt->sndBuffer.c_str(), clnt->sndBuffer.length(), 0 ) == SOCKET_ERROR ) {closesocket ( clnt->clientsocket );    clnt->connected = 0;    clnt->hThreadHandle = NULL;    clnt->dwThreadID = NULL;    clnt->rcvBuffer = "";    clnt->sndBuffer = "";    break;}clnt->sndBuffer = "";}}}// The call to _beginthreadexif ( ( clnt.hThreadHandle = (HANDLE)_beginthreadex ( NULL, NULL, (LPTHREAD_START_ROUTINE)&vSocketThread, &clnt, NULL, clnt.dwThreadID ) ) == NULL ) {    std::cout << "error with thread" << std::endl;} [Edited by - rogierpennink on June 18, 2006 6:17:53 AM];  as i see it , param 3 is a pointer to a the starting function address. the err states that problemsoccured in conversion. from the msdn : "The _beginthread function creates a thread that begins execution of a routine at start_address. The routine at start_address must use the __cdecl calling convention and should have NO RETRUN VALUE. When the thread returns from that routine, it is terminated automatically. " so i reckon thatyou should set the return value to void.   ;  Hi, first of all I'm not a pro of Thread, but I'v done some at school this session, so i might be wrong.As i can see, your thread functionunsigned __stdcall vSocketThread ( CLIENT *clnt )takes a CLIENT as parameter. As far as i know, I believe that win32 thread only takes void* as a unique parameter. So instead of recieving a CLIENT change it for a void*Then do a reinterpret_cast and store it in a CLIENT objet and use this objet as normaly.I think this is where your error come from.   