CreateThread in Windows

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sevak316
    New Member
    • Sep 2008
    • 73

    CreateThread in Windows

    This must be an easy question.

    I am trying to create a thread in windows. I am using C as my language.

    I am trying to pass a function to the CreateThread function to create a thread. My function has a return type void and paratmeter is void as well. How do I approach this?

    Is it something like this?

    static void run() {
    myThread = CreateThread(
    NULL,
    0,
    MyFunction,
    NULL,
    0,
    NULL);
    }

    How do I pass my function to CreateThread?

    What is this DWORD WINAPI functionName () that I am reading about? Do I need this function?

    Thanks in advance,
    Sevak.
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    The function you pass to CreateThread must return a DWORD and be defined with the WINAPI keyword. For example, [CODE=c]DWORD WINAPI run(void* arg); //LPVOID would be more "Windows", not that I really care[/CODE]

    Comment

    • sevak316
      New Member
      • Sep 2008
      • 73

      #3
      sorry, but im confused. I have been lookin at code ALL day!

      who ends up calling my funtion and how??
      lets say my function is "void xTaskFast(void) "

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        Then you call CreateThread as follows:

        [CODE=c]
        DWORD tid;
        HANDLE h = CreateThread(NU LL, 0, xFastTask, 0, &tid); //check h to see if it succeeded or not
        [/CODE]

        Source: MSDN

        The spawned thread, if the call is successful, will be created and will start execution at the beginning of the function passed in the third parameter. The thread that called CreateThread will continue from the next line (this should be error checking).

        Comment

        Working...