Wiki Page Content

Differences between revisions 12 and 13
Revision 12 as of 2013-11-16 13:30:29
Size: 1969
Comment: Changed includes in example, see FAQDevelopment page.
Revision 13 as of 2013-12-07 14:38:35
Size: 1860
Comment: Changed spaces in example to reduce needed size.
Deletions are marked like this. Additions are marked like this.
Line 28: Line 28:
int TestThread( void *ptr ); int TestThread(void *ptr);
Line 32: Line 32:
     SDL_Thread *thread;
        SDL_threadID threadID;
     int threadReturnValue;
    SDL_Thread *thread;
    SDL_threadID threadID;
    int threadReturnValue;
Line 36: Line 36:
     printf("\nSimple SDL_CreateThread test:");     printf("\nSimple SDL_CreateThread test:");
Line 38: Line 38:
     // Simply create a thread
     thread = SDL_CreateThread(TestThread, "TestThread", (void *)NULL);
    // Simply create a thread
    thread = SDL_CreateThread(TestThread, "TestThread", (void *)NULL);
Line 41: Line 41:
     if( NULL == thread )
       
{
    printf("\nSDL_CreateThread failed: %s\n", SDL_GetError());
           exit(-1);
        }
    if (NULL == thread) {
        printf("\nSDL_CreateThread failed: %s\n", SDL_GetError());
        exit(-1);
    }
Line 47: Line 46:
     // Retrieve the ID for the newly launched thread
        threadID = SDL_GetThreadID( thread );
    // Retrieve the ID for the newly launched thread
    threadID = SDL_GetThreadID(thread);
Line 50: Line 49:
     // Wait for the thread to complete and get the return code
        SDL_WaitThread( thread, &threadReturnValue);
     printf("\nThread returned value:%d", threadReturnValue);
    // Wait for the thread to complete and get the return code
    SDL_WaitThread(thread, &threadReturnValue);
    printf("\nThread returned value:%d", threadReturnValue);
Line 54: Line 53:
     return 1;     return 1;
Line 60: Line 59:
     int cnt;     int cnt;
Line 62: Line 61:
     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 68: Line 66:
     return cnt;     return cnt;
Line 71: Line 69:

SDL_GetThreadID

Use this function to get the thread identifier for the specified thread.

Syntax

SDL_threadID SDL_GetThreadID(SDL_Thread* thread)

Function Parameters

thread

The thread to query

Return Value

Returns the ID of the specified thread, or the ID of the current thread if thread is NULL.

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;
    SDL_threadID threadID;
    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());
        exit(-1);
    }

    // Retrieve the ID for the newly launched thread
    threadID = SDL_GetThreadID(thread);

    // Wait for the thread to complete and get the return code
    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;
}

Remarks

This thread identifier is as reported by the underlying operating system. If SDL is running on a platform that does not support threads the return value will always be zero.


CategoryAPI, CategoryThread

None: SDL_GetThreadID (last edited 2017-03-11 22:48:55 by PhilippWiesemann)

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