add source code
This commit is contained in:
		
							
								
								
									
										14
									
								
								CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
cmake_minimum_required(VERSION 3.16)
 | 
			
		||||
 | 
			
		||||
project(sdl-thread LANGUAGES C)
 | 
			
		||||
 | 
			
		||||
find_package(SDL2 REQUIRED)
 | 
			
		||||
add_executable(sdl-thread main.c)
 | 
			
		||||
 | 
			
		||||
include(GNUInstallDirs)
 | 
			
		||||
install(TARGETS sdl-thread
 | 
			
		||||
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
 | 
			
		||||
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2::SDL2)
 | 
			
		||||
							
								
								
									
										54
									
								
								main.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								main.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,54 @@
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <SDL2/SDL.h>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
SDL_mutex *s_lock = NULL;
 | 
			
		||||
SDL_cond *s_cond = NULL;
 | 
			
		||||
 | 
			
		||||
int work_thread(void *arg) {
 | 
			
		||||
    SDL_LockMutex(s_lock);
 | 
			
		||||
    printf("<=========work_thread sleep=========>\n");
 | 
			
		||||
    sleep(10);
 | 
			
		||||
    printf("<=========work_thread wait condition=========>\n");
 | 
			
		||||
    SDL_CondWait(s_cond, s_lock);
 | 
			
		||||
    printf("<=========work_thread receive signal=========>\n");
 | 
			
		||||
    printf("<=========work_thread end=========>\n");
 | 
			
		||||
    SDL_UnlockMutex(s_lock);
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#undef main
 | 
			
		||||
int main()
 | 
			
		||||
{
 | 
			
		||||
    s_lock = SDL_CreateMutex();
 | 
			
		||||
    s_cond = SDL_CreateCond();
 | 
			
		||||
    SDL_Thread *t = SDL_CreateThread(work_thread, "work_thread", NULL);
 | 
			
		||||
 | 
			
		||||
    if (!t) {
 | 
			
		||||
        printf("SDL_CreateThread() failed\n");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (int i = 0; i < 2; i++) {
 | 
			
		||||
        sleep(2);
 | 
			
		||||
        printf("main execute\n");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    printf("SDL_LockMutex(s_lock) before\n");
 | 
			
		||||
    SDL_LockMutex(s_lock);
 | 
			
		||||
    printf("SDL_LockMutex(s_lock) after\n");
 | 
			
		||||
    printf("SDL_CondSignal(s_cond) before\n");
 | 
			
		||||
    SDL_CondSignal(s_cond);
 | 
			
		||||
    printf("SDL_CondSignal(s_cond) after\n");
 | 
			
		||||
    sleep(10);
 | 
			
		||||
    printf("SDL_UnlockMutex(s_lock) before\n");
 | 
			
		||||
    SDL_UnlockMutex(s_lock);
 | 
			
		||||
    printf("SDL_UnlockMutex(s_lock) after\n");
 | 
			
		||||
 | 
			
		||||
    SDL_WaitThread(t, NULL);
 | 
			
		||||
    SDL_DestroyMutex(s_lock);
 | 
			
		||||
    SDL_DestroyCond(s_cond);
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user