Wiki Page Content

Differences between revisions 9 and 10
Revision 9 as of 2012-08-06 10:11:09
Size: 2584
Editor: pallavnawani
Comment: Added information about the memory pointed to by the 'thread' parameter.
Revision 10 as of 2013-08-08 19:27:42
Size: 2276
Editor: Sam Lantinga
Comment:
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
||<tablewidth="100%"style="color: rgb(255, 0, 0); text-align: center;">DRAFT ||
Line 7: Line 5:
Use this function to wait for a thread to finish. As a side effect, the function will free the memory allocated to the SDL_Thread structure. Use this function to wait for a thread to finish.
Line 17: Line 15:
||'''thread''' ||The SDL_Thread pointer that was returned from the SDL_CreateThread call that started this thread. Note that the pointer becomes invalid after SDL_WaitThread finishes, because this function will free the memory pointed to. || ||'''thread''' ||The SDL_Thread pointer that was returned from the [[SDL_CreateThread]]() call that started this thread.||
Line 70: Line 68:
Line 71: Line 70:
The return code for the thread function is placed in the area pointed to by '''status''', if '''status''' is not NULL. The return code is the value returned by the 'return' call in the thread function. This can be a 'status' on the thread, or the result of a computation the thread was created to perform. The return code for the thread function is placed in the area pointed to by '''status''', if '''status''' is not NULL. The return code is the value returned by the 'return' statement in the thread function.
Line 73: Line 72:
Note that the thread pointer is freed by this function and is not valid afterward.
Line 75: Line 75:
 . [[SDL_CreateThread]] *  . [[SDL_CreateThread]]

SDL_WaitThread

Use this function to wait for a thread to finish.

Syntax

void SDL_WaitThread(SDL_Thread* thread,
                    int*        status)

Function Parameters

thread

The SDL_Thread pointer that was returned from the SDL_CreateThread() call that started this thread.

status

Pointer to an integer that will receive the value returned from the thread function by its 'return', see Remarks for details

Code Examples

Toggle line numbers

#include <stdio.h>
#include <SDL_thread.h>
#include <SDL_timer.h>

int TestThread( void *ptr );

int main(int argc, char *argv[])
{
        SDL_Thread *thread;
        int         threadReturnValue;

        printf("\nSimple SDL_CreateThread test:");

        // Simply create a thread
        thread = SDL_CreateThread( TestThread, (void *)NULL);

        if( NULL == thread )
           printf("\nSDL_CreateThread failed: %s\n", SDL_GetError());
        else
        {

           // Wait for the thread to complete. The thread functions return code will
           //       be placed in the "threadReturnValue" variable when it completes.
           //
           SDL_WaitThread( thread, &threadReturnValue);
           printf("\nThread returned value:%d", threadReturnValue);
        }

        return 1;
}

// Very simple thread - counts 0 to 9 delaying 50ms between increments
int TestThread( void *ptr )
{
        int        cnt;

        for(cnt=0;cnt<10;cnt++)
        {
                printf( "\nThread counter: %d", cnt);
                SDL_Delay(50);
        }

        // Return the final value to the SDL_WaitThread function above
        return cnt;
}

Remarks

The return code for the thread function is placed in the area pointed to by status, if status is not NULL. The return code is the value returned by the 'return' statement in the thread function.

Note that the thread pointer is freed by this function and is not valid afterward.


CategoryAPI, CategoryThread

None: SDL_WaitThread (last edited 2018-12-12 14:53:52 by RyanGordon)

Feedback
Please include your contact information if you'd like to receive a reply.
Submit