Wiki Page Content

Differences between revisions 11 and 12
Revision 11 as of 2013-10-05 20:36:26
Size: 2275
Comment: Changed descriptions, see SGFunctions page.
Revision 12 as of 2013-10-05 20:43:25
Size: 2147
Comment: Removed and added spaces in example.
Deletions are marked like this. Additions are marked like this.
Line 20: Line 20:
Toggle line numbers
Line 26: Line 25:
int TestThread( void *ptr ); int TestThread(void *ptr);
Line 30: Line 29:
     SDL_Thread *thread;
     int threadReturnValue;
    SDL_Thread *thread;
    int threadReturnValue;
Line 33: Line 32:
     printf("\nSimple SDL_CreateThread test:");     printf("\nSimple SDL_CreateThread test:");
Line 35: Line 34:
     // Simply create a thread
        thread = SDL_CreateThread( TestThread, (void *)NULL);
    // Simply create a thread
    thread = SDL_CreateThread(TestThread, (void *)NULL);
Line 38: Line 37:
        if( NULL == thread )
           printf("\nSDL_CreateThread failed: %s\n", SDL_GetError());
        else
        {
    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);
    }
Line 43: Line 47:
           // 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;
    return 1;
Line 54: Line 51:
int TestThread( void *ptr ) int TestThread(void *ptr)
Line 56: Line 53:
     int cnt;     int cnt;
Line 58: Line 55:
     for(cnt=0;cnt<10;cnt++)
       
{
         printf( "\nThread counter: %d", cnt);
                SDL_Delay(50);
        }
    for (cnt = 0; cnt < 10; cnt++) {
        printf("\nThread counter: %d", cnt);
        SDL_Delay(50);
    }
Line 64: Line 60:
     // Return the final value to the SDL_WaitThread function above
        return cnt;
    // Return the final value to the SDL_WaitThread function above
    return cnt;

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

#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