Wiki Page Content

Differences between revisions 21 and 22
Revision 21 as of 2013-11-16 13:29:33
Size: 3090
Comment: Changed includes in example, see FAQDevelopment page.
Revision 22 as of 2014-01-11 13:58:20
Size: 2991
Comment: Removed and added spaces in example, changed indent style.
Deletions are marked like this. Additions are marked like this.
Line 31: Line 31:
int TestThread( void *ptr ); int TestThread(void *ptr);
Line 35: Line 35:
     SDL_Thread *thread;
     int threadReturnValue;
    SDL_Thread *thread;
    int threadReturnValue;
Line 38: Line 38:
     printf("\nSimple SDL_CreateThread test:");     printf("\nSimple SDL_CreateThread test:");
Line 40: Line 40:
     // Simply create a thread
        thread = SDL_CreateThread( TestThread, "TestThread", (void *)NULL);
    // Simply create a thread
    thread = SDL_CreateThread(TestThread, "TestThread", (void *)NULL);
Line 43: Line 43:
        if( NULL == thread )
   
printf("\nSDL_CreateThread failed: %s\n", SDL_GetError());
        else
       
{
    SDL_WaitThread( thread, &threadReturnValue);
    printf("\nThread returned value:%d", threadReturnValue);
        }
    if (NULL == thread) {
printf("\nSDL_CreateThread failed: %s\n", SDL_GetError());
    } else {
        SDL_WaitThread(thread, &threadReturnValue);
        printf("\nThread returned value:%d", threadReturnValue);
    }
Line 51: Line 50:
     return 1;     return 1;
Line 55: Line 54:
int TestThread( void *ptr ) int TestThread(void *ptr)
Line 57: Line 56:
     int        cnt;     int cnt;
Line 59: Line 58:
     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 65: Line 63:
     return cnt;     return cnt;

SDL_CreateThread

Use this function to create a new thread.

Syntax

SDL_Thread* SDL_CreateThread(SDL_ThreadFunction fn,
                             const char*        name,
                             void*              data)

Function Parameters

fn

the function to call in the new thread; see Remarks for details

name

the name of the thread; see Remarks for details

data

a pointer that is passed to fn

Return Value

Returns an opaque pointer to the new thread object on success, NULL if the new thread could not be created; call SDL_GetError() for more information.

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 {
        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 cnt;
}

Output:

Simple SDL_CreateThread test:
Thread counter: 0
Thread counter: 1
Thread counter: 2
Thread counter: 3
Thread counter: 4
Thread counter: 5
Thread counter: 6
Thread counter: 7
Thread counter: 8
Thread counter: 9
Thread returned value:10

Remarks

SDL_CreateThread() creates a new thread of execution that shares all of its parent's global memory, signal handlers, file descriptors, etc, and runs the function fn, passing it the void pointer data. The thread quits when fn returns.

The function prototype for fn is:

int SDL_ThreadFunction(void* data)
  • where its parameter is:

There are no requirements for thread naming conventions, so long as the string is null-terminated UTF-8, but these guidelines are helpful in choosing a name:

If a system imposes requirements, SDL will try to munge the string for it (truncate, etc), but the original string contents will be available from SDL_GetThreadName().


CategoryAPI, CategoryThread

None: SDL_CreateThread (last edited 2019-01-21 19:12:12 by haxpor)

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