Wiki Page Content

Differences between revisions 15 and 18 (spanning 3 versions)
Revision 15 as of 2013-11-14 06:31:33
Size: 2703
Editor: RyanGordon
Comment: wikified
Revision 18 as of 2014-01-11 19:12:44
Size: 2706
Comment: Added a space in example and changed return value.
Deletions are marked like this. Additions are marked like this.
Line 22: Line 22:
#include <SDL_thread.h>
#include <SDL_timer.h>
#include "SDL_thread.h"
#include "SDL_timer.h"
Line 44: Line 44:
        printf("\nThread returned value:%d", threadReturnValue);         printf("\nThread returned value: %d", threadReturnValue);
Line 47: Line 47:
    return 1;     return 0;
Line 53: Line 53:
    int        cnt;     int cnt;
Line 75: Line 75:
It is safe to pass NULL to this function; it is a no-op. It is safe to pass a NULL thread to this function; it is a no-op.


Use this function to wait for a thread to finish.


void SDL_WaitThread(SDL_Thread* thread,
                    int*        status)

Function Parameters


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


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

Code Examples

#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, "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 0;

// 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);

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


Wait for a thread to finish. Threads that haven't been detached will remain (as a "zombie") until this function cleans them up. Not doing so is a resource leak.

Once a thread has been cleaned up through this function, the SDL_Thread that references it becomes invalid and should not be referenced again. As such, only one thread may call SDL_WaitThread() on another.

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

You may not wait on a thread that has been used in a call to SDL_DetachThread(). Use either that function or this one, but not both, or behavior is undefined.

It is safe to pass a NULL thread to this function; it is a no-op.

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)

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