[$BA0(B]
$BL\
[$B]
Simple DirectMedia Layer API $B$r;H$&(B
$B%9%l%C%I(B
$B%9%l%C%I$N@8@.$O(B SDL_CreateThread() $B$K4X?t$rEO$9$3$H$G$J$5$l$^$&!#(B
$B4X?t$+$iLa$C$?$H$-!"[email protected]$7$F$$$l$P!"(B
$B$"$J$?$N4X?t$O%"%W%j%1!<%7%g%s$NB>$NItJ,$HJB9T$7$F(B
$B<+?H$N$NItJ,$G;H$o$l$?%a%b%j$d(B
$B%U%!%$%k%O%s%I%k$K%"%/%;%9$9$k$3$H$,$G$-$^$9!#(B
|
Tip:
SDL_CreateThread() $B$N(B 2 $BHVL\$N0z?t$O%9%l%C%I4X?t$K%Q%i%a!<%?$H$7$FEO$5$l$^$9!#(B
$B%9%?%C%/>e$NCM$d%9%l%C%I$G;H$o$l$k%G!<%?$X$N%]%$%s%?$rEO$9$?$a$K(B
$B;H$&$3$H$,$G$-$^$9!#(B |
Example:
#include "SDL_thread.h"
int global_data = 0;
int thread_func(void *unused)
{
int last_value = 0;
while ( global_data != -1 ) {
if ( global_data != last_value ) {
printf("Data value changed to %d\n", global_data);
last_value = global_data;
}
SDL_Delay(100);
}
printf("Thread quitting\n");
return(0);
}
{
SDL_Thread *thread;
int i;
thread = SDL_CreateThread(thread_func, NULL);
if ( thread == NULL ) {
fprintf(stderr, "Unable to create thread: %s\n", SDL_GetError());
return;
}
for ( i=0; i<5; ++i ) {
printf("Changing value to %d\n", i);
global_data = i;
SDL_Delay(1000);
}
printf("Signaling thread to quit\n");
global_data = -1;
SDL_WaitThread(thread, NULL);
}
|
- $B%j%=!<%9$X$NF14|%"%/%;%9(B
2 $B$D0J>e$N%9%l%C%I$+$i%j%=!<%9$X$N%"%/%;%9$r6X;_$9$k$3$H$,$G$-$^$9!#(B
$B$=$l$K$O!"$^$:(B mutex $B$r:n@.$7!"%j%=!<%9$X$N%"%/%;%9$r(B
$B%m%C%/(B(SDL_mutexP())$B$H%"%s%m%C%/(B(SDL_mutexV())$B$G0O$_$^$9!#(B
|
Tip:
2 $B$D0J>e$N%9%l%C%I$+$i%"%/%;%9$5$l$&$k%G!<%?$O(B
$BA4$F(B mutex $B$GJ]8n$5$l$kI,MW$,$"$j$^$9!#(B |
Example:
#include "SDL_thread.h"
#include "SDL_mutex.h"
int potty = 0;
int gotta_go;
int thread_func(void *data)
{
SDL_mutex *lock = (SDL_mutex *)data;
int times_went;
times_went = 0;
while ( gotta_go ) {
SDL_mutexP(lock); /* potty($B$*$^$k(B)$B$r%m%C%/(B */
++potty;
printf("Thread %d using the potty\n", SDL_ThreadID());
if ( potty > 1 ) {
printf("Uh oh, somebody else is using the potty!\n");
}
--potty;
SDL_mutexV(lock);
++times_went;
}
printf("Yep\n");
return(times_went);
}
{
const int progeny = 5;
SDL_Thread *kids[progeny];
SDL_mutex *lock;
int i, lots;
/* $BF14|%m%C%/$r:n@.(B */
lock = SDL_CreateMutex();
gotta_go = 1;
for ( i=0; i<progeny; ++i ) {
kids[i] = SDL_CreateThread(thread_func, lock);
}
SDL_Delay(5*1000);
SDL_mutexP(lock);
printf("Everybody done?\n");
gotta_go = 0;
SDL_mutexV(lock);
for ( i=0; i<progeny; ++i ) {
SDL_WaitThread(kids[i], &lots);
printf("Thread %d used the potty %d times\n", i+1, lots);
}
SDL_DestroyMutex(lock);
}
|
[$BA0(B]
$BL\
[$B]
|