Thread: Threading problem

  1. #1
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470

    Threading problem

    hi im trying to create a server program to recieve and send text msgs to and from clients. im trying to create a seperate thread for the function that listens to clients, so that the server can also send data while it listen. i create the thread like this:

    Code:
    void CServerDlg::OnBnClickedBstart()
    {
        HANDLE hT1 =  CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ListenToClient,NULL,0,NULL); 
        WaitForSingleObject(hT1, INFINITE);
    }
    but when thread starts, the program hangs (because of the infinite loop inside the ListenToClient() function). (ListenToClient() is not a class function.) so creating a different thread like this dosen't seems to work with MFC? so how can i run ListenToClient() seperately so it dosen't hangs programs' UI?

    thanks for any help.
    GameJolt: https://gamejolt.com/@KasunL
    Game Development Youtube:
    https://is.gd/XyhYoP
    Amateur IT Blog: http://everything-geeky.blogspot.com/



    (and, sorry for my amateur English)

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Is this a blocking socket?

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    the program hangs (because of the infinite loop inside the ListenToClient() function).
    Programs hans up because you do not exit the OnClicked callack till the created thread is finiished. Remove you waitforsingleObject - It just kills the idea of creating thread
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470
    ok thanx.
    GameJolt: https://gamejolt.com/@KasunL
    Game Development Youtube:
    https://is.gd/XyhYoP
    Amateur IT Blog: http://everything-geeky.blogspot.com/



    (and, sorry for my amateur English)

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    1) Do NOT cast the address you pass to CreateThread. If it doesn't compile without it, then your prototype function is incorrect.
    2) For MFC, you should use AfxBeginThread and not CreateThread (I'm assuming you are?).
    3) And yes, you're basically blocking until the thread exits.
    Last edited by Elysia; 12-14-2007 at 06:25 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    3) To actual way to get the address of a function is to do &Function and not Function; this is non-standard and supported in versions prior to Visual Studio 2005.
    The above is incorrect.

    Code:
    D:\temp>gcc -Wall -pedantic -ansi t.c
    
    D:\temp>cat t.c
    #include <stdio.h>
    void foo(void) {
       printf("In foo\n");
    }
    void (*f)(void) = foo;
    
    int main()
    {
       f();
      return 0;
    }
    
    D:\temp>a
    In foo
    No ampersand needed to take an address of a function. [This is why it also works to write "foo;" in a line of code, and at worst(best?), this gives a warning for "statement with no effect"].

    I tried with g++ too, just in case.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    From what I understand, Visual C++ 2005 was made to be more standards compliant.
    Maybe it just applies to classes? I'll have to try.
    From what I see, it only applies to class members. I'd have preferred FunctionName wouldn't work, but apparently it does...
    Last edited by Elysia; 12-14-2007 at 06:25 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM