#include #include #define ADD_MUTEX_SAFETY Uint8 run; #ifdef ADD_MUTEX_SAFETY SDL_mutex *mutex; #endif int Load(void* data) { while (run) { #ifdef ADD_MUTEX_SAFETY SDL_LockMutex(mutex); #endif SDL_Surface *sur = IMG_Load("blue.png"); SDL_Log("%lu %lX\n", SDL_ThreadID(), sur); SDL_FreeSurface(sur); #ifdef ADD_MUTEX_SAFETY SDL_UnlockMutex(mutex); #endif } } int main(int argc, char **argv) { SDL_Thread **thread; SDL_Event evt; int cpus, i; SDL_Init(SDL_INIT_EVENTS); #ifdef ADD_MUTEX_SAFETY mutex = SDL_CreateMutex(); #endif run = 1; cpus = SDL_GetCPUCount() - 1; thread = (SDL_Thread**) SDL_malloc(sizeof(SDL_Thread*) * cpus); for (i = 0; i < cpus; i += 1) { thread[i] = SDL_CreateThread(Load, NULL, NULL); } while (run) { while (SDL_PollEvent(&evt) > 0) { if (evt.type == SDL_QUIT) { run = 0; } } } for (i = 0; i < cpus; i += 1) { SDL_WaitThread(thread[i], NULL); } #ifdef ADD_MUTEX_SAFETY SDL_DestroyMutex(mutex); #endif SDL_free(thread); SDL_Quit(); }