CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: thread

  1. #1
    Join Date
    Apr 2002
    Location
    Switzerland
    Posts
    31

    thread

    Hi
    I wanna program a client/server-app. Each client should start an own thread on the server.

    I've tried already something, but it doesnt work properly. I got some problems with this "_beginthread"-function.


    Code:
       Socket s;
       SOCKET* newConnection = 0;
    
       while(1)
       {
          newConnection = s.acceptConnection();
          Service myService(newConnection);
    
          _beginthread((void (*)(void *))myService.start(), 0, (void * newConnection);
        
       }


    Any better ideas ?


    Thanks
    Flavio

  2. #2
    Join Date
    Aug 2001
    Location
    North Bend, WA
    Posts
    1,947
    I'm suprised it even compiles. Your 1st and second arguments look pretty screwed up.

    _beginthread((void (*)(void *))myService.start(), 0, (void * newConnection);


    What is (void (*)(void *))myService.start(), supposed to mean?

    What is this supposed to do? (void * newConnection

    What error do you get?

    Try looking at the example in MSDN. It is very different from your code.

  3. #3
    Join Date
    Apr 2002
    Location
    Switzerland
    Posts
    31
    ok i've given a look at msdn and i coded it with the "CreateThread"-function. It works fine now, but i've still a problem:

    I can compile it, but i cant link it. there's a problem with the parameter "newConnection" which I wanna give to the thread-function:

    well, here's the code:

    Code:
    DWORD WINAPI myThread(LPVOID);
    I guess, LPVOID is wrong, but when I try with SOCKET* (which is my parameter I wanna pass) it doesnt work too.
    Code:
    void
    main()
    {
    
       DWORD dwThreadId[100];
       HANDLE hThread[100]; 
       int i = 0;
    
    
       while(1)
       {
            Socket s;
            SOCKET* newConnection = 0;
            newConnection = s.acceptConnection();
    
          
            hThread[i] = CreateThread( 
            NULL,                          // default security attributes 
            0,                                // use default stack size  
            myThread,                  // thread function 
            newConnection,         // argument to thread function 
            0,                               // use default creation flags 
            &dwThreadId[i]);       // returns the thread identifier 
    
            i++;
            Sleep(100);
       }
    
    
    
    DWORD WINAPI myThread(SOCKET* newConnection)
    {
    
       Service myService(newConnection);
       return EXIT_SUCCESS;
    }
    ok, thanks very much

    Flavio

  4. #4
    Join Date
    Jul 2001
    Location
    Florida, USA
    Posts
    107
    You do want to use the LPVOID. Inside your myThread function you will have to use a cast to get the parameter to the variable type that you want.

    For example

    Code:
    DWORD WINAPI myThread(LPVOID newConnection){
    Service myService((SOCKET*)newConnection);
       return EXIT_SUCCESS;
    }
    I hope this helps somewhat.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured