Thread: Question about creating threads

  1. #1
    Registered User
    Join Date
    Jun 2008

    Question about creating threads

    I was a little confused after reading the stuff on MSDN about CreateThread, ThreadProc etc.

    1. When I create a thread, does it keep running over and over until ExitThread is called, or does returning any value kill it? The only one I've seen in action used a while(1) infinite loop and apparently returned when done or on error.

    2. After calling CreateThread, can you call the ThreadProc(&lpParameter) to send it commands etc, just as if you were calling SendMessage on a callback for a window? I kind of wondered why that's a single pointer instead of allowing you to declare the ThreadProc with whatever vars you want it to receive (like window callbacks get HWND, MSG, LPARAM, WPARAM).

    Basically, what I'd like to do is create a client thread for a winsock app to connect, if necessary reconnect, and handle sending/receiving socket data. It's a single connection that should stay open as long as the app is running, but if the other side is disconnected for some reason it needs to keep trying to reconnect. The reason for keeping it a separate thread is mainly that some of the data being transferred can take a long time. I suppose it's similar to writing an FTP app, but this is always connecting to the same place, automatically.
    Last edited by Viper187; 07-25-2009 at 02:47 PM.

  2. #2
    Registered User
    Join Date
    Mar 2007
    Threads do not continuously run. They are created and exit just like a function. I made a class to work with UDP sockets and mine creates a thread to receive data. While the thread is receiving data then do your other stuff. When the thread exits then call the thread again in a loop, or statement, or whatever you want. Basically have a loop calling the thread again when it exits.

    I think the function I used to create my threads was _beginthread(). It's been a while since I looked at it.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Thanks. I'll see what I can put together.

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    ThreadProc is the function that the thread starts in. When the thread reaches the end of that function, it terminates.
    And because the Thread API is C, and the thread API needs to take a function pointer to the thread to start execution in, it must be a pre-defined prototype.

    And just to be clear - all the API that requires a function pointer requires a specific type of function. It must be the correct return type and the correct number of parameters and types. You cannot choose what they should take and not.
    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.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Well, I came up with a method to suit my purpose for now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating handheld video games(odd question)
    By nubby in forum Tech Board
    Replies: 0
    Last Post: 03-11-2008, 04:18 PM
  2. simple question on creating a window
    By black in forum Windows Programming
    Replies: 5
    Last Post: 12-29-2005, 12:10 PM
  3. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 03:52 PM
  4. problem with win32 threads
    By pdmarshall in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2004, 02:39 PM
  5. Threads terminate when parent process does?
    By BigDaddyDrew in forum Windows Programming
    Replies: 1
    Last Post: 04-21-2004, 04:32 PM