Threads in SDL
I did but i dont understand how to pass variables and what exactly is hapenning when you initialize a thread. Any help would be appreciated.
from the header file:
extern DECLSPEC SDL_Thread * SDLCALL SDL_CreateThread( int (*fn)(void *), void *data);
let''s disect this...
it returns a pointer to ''SDL_Thread''. Usefull for keeping track of threads from the parent if you need to do that.
it accepts two parameters.
The first parameter is a pointer to a function. Specifically, a function returning an int, and taking a void pointer. Not void, a void pointer.
The second parameter is a void pointer as well.
Can you guess the answer?
if not...:
when you create the thread, you can pass a single pointer to it. If you need to send a good deal of information, then pack that all up into a structure or something, and then cast a pointer to that to void and pass that as the ''data'' variable in the SDL_CreateThread function call.
Then, in the called function, cast the void pointer you get back to a pointer to the structure type, which you can dereference to get to all that nifty data.
aka something like:
extern DECLSPEC SDL_Thread * SDLCALL SDL_CreateThread( int (*fn)(void *), void *data);
let''s disect this...
it returns a pointer to ''SDL_Thread''. Usefull for keeping track of threads from the parent if you need to do that.
it accepts two parameters.
The first parameter is a pointer to a function. Specifically, a function returning an int, and taking a void pointer. Not void, a void pointer.
The second parameter is a void pointer as well.
Can you guess the answer?
if not...:
when you create the thread, you can pass a single pointer to it. If you need to send a good deal of information, then pack that all up into a structure or something, and then cast a pointer to that to void and pass that as the ''data'' variable in the SDL_CreateThread function call.
Then, in the called function, cast the void pointer you get back to a pointer to the structure type, which you can dereference to get to all that nifty data.
aka something like:
#include <iostream>#include <string>#include <SDL/SDL.h>using namespace std;class Something{ public: string say;};int test( void *param ){ cout << ((Something *)param)->say << endl; return 0;}int main( ... ){ //..SDL SETUP CODE... Something *mysomething = new Something; mysomething->say = "WOOT!!!"; SDL_CreateThread( test , (void*) mysomething ); //..SDL CLEANUP CODE...}
SDL threads are just there or provide thread functionality. The documentation was not intended to be general purpose tutorial on threads, but rather a tutorial on how SDL implements threads.
To learn about threads read these two tutorials:
http://www.cpp-home.com/tutorial.php?124_2 and
http://www.cpp-home.com/tutorial.php?128_1.
The first tut is general theory on threads in general. The second give a practical implementation of threads using a thread library, simliar to the SDL threads library.
To learn about threads read these two tutorials:
http://www.cpp-home.com/tutorial.php?124_2 and
http://www.cpp-home.com/tutorial.php?128_1.
The first tut is general theory on threads in general. The second give a practical implementation of threads using a thread library, simliar to the SDL threads library.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement