SDL事件处理以及线程使用(2)

事件使用

cpp 复制代码
#include <stdio.h>
#include <SDL.h>

#define FF_QUIT_EVENT (SDL_USEREVENT + 1)       // 定义自定义事件

#undef main
int main()
{
    SDL_Window* pWindow = NULL;

    SDL_Init(SDL_INIT_VIDEO);

    // 创建窗口
    pWindow = SDL_CreateWindow("Event Test Title",
                               SDL_WINDOWPOS_UNDEFINED,
                               SDL_WINDOWPOS_UNDEFINED,
                               640,
                               480,
                               SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
    if(NULL == pWindow)
    {
        printf("Create window error: %s\n", SDL_GetError());
        return -1;
    }

    SDL_Event event;
    int is_exit = 1;
    while(is_exit)
    {
        SDL_WaitEvent(&event);
        switch (event.type)             // 区分鼠标事件、键盘事件
        {
        case SDL_KEYDOWN:               // 键盘按下事件
            switch (event.key.keysym.sym)
            {
                case SDLK_a:
                    break;
                case SDLK_s:
                    break;
                case SDLK_d:
                    break;
                case SDLK_w:
                    break;
                case SDLK_q:{
                    SDL_Event myEvent;              // 自定义事件并发送
                    myEvent.type = FF_QUIT_EVENT;
                    SDL_PushEvent(&myEvent);
                    break;
                }
            }
            break;
        case SDL_MOUSEBUTTONDOWN:           // 鼠标按键按下事件
            switch(event.button.button)
            {
            case SDL_BUTTON_LEFT:
                printf("mouse button left down\n");
                break;
            case SDL_BUTTON_RIGHT:
                printf("mouse button right down\n");
                break;
            }
            break;
        case SDL_MOUSEMOTION:           // 鼠标移动事件
            printf("mouse move {%d, %d}\n", event.button.x, event.button.y);
            break;
        case FF_QUIT_EVENT:             // 自定义事件
            printf("myself event is triggered\n");
            is_exit = 0;
            break;
        }
    }

    // 销毁窗口,释放资源
    if(pWindow)
        SDL_DestroyWindow(pWindow);
    SDL_Quit();

    return 0;
}

线程使用

cpp 复制代码
#include <SDL.h>
#include <stdio.h>

int n = 0;
// 线程处理函数
int thread_work(void* arg)
{
    printf("enter thread arg: %d\n", *((int*)arg));
    for(int i = 0; i < 500000; i++)
    {
        n++;
    }
    return 0;
}

#undef main
int main()
{
    int thread_num = 520;
    // 创建线程
    SDL_Thread* thread1 = SDL_CreateThread(thread_work, "thread1Name", &thread_num);
    if(NULL == thread1)
    {
        printf("thread1 create error %s\n", SDL_GetError());
        return -1;
    }
    thread_num++;
    SDL_Thread* thread2 = SDL_CreateThread(thread_work, "thread2Name", &thread_num);
    if(NULL == thread2)
    {
        printf("thread2 create error: %s\n", SDL_GetError());
        return -1;
    }

    // 等待线程汇入主线程
    SDL_WaitThread(thread1, NULL);
    SDL_WaitThread(thread2, NULL);
    printf("n is value: %d\n", n);

    return 0;
}

互斥锁使用

cpp 复制代码
#include <SDL.h>
#include <stdio.h>

/// 互斥锁 ///
SDL_mutex* mutex = NULL;
/

int n = 0;
// 线程处理函数
int thread_work(void* arg)
{
    printf("enter thread arg: %d\n", *((int*)arg));
    /// 上锁 ///
    SDL_LockMutex(mutex);
    /
    for(int i = 0; i < 500000; i++)
    {
        n++;
    }/// 解锁 ///
    SDL_UnlockMutex(mutex);
    /
    return 0;
}

#undef main
int main()
{
    /// 创建互斥锁 ///
    mutex = SDL_CreateMutex();
    if(NULL == mutex)
    {
        printf("Create mutex error: %s\n", SDL_GetError());
        return -1;
    }
    /

    int thread_num = 520;
    // 创建线程
    SDL_Thread* thread1 = SDL_CreateThread(thread_work, "thread1Name", &thread_num);
    if(NULL == thread1)
    {
        printf("thread1 create error %s\n", SDL_GetError());
        return -1;
    }
    thread_num++;
    SDL_Thread* thread2 = SDL_CreateThread(thread_work, "thread2Name", &thread_num);
    if(NULL == thread2)
    {
        printf("thread2 create error: %s\n", SDL_GetError());
        return -1;
    }

    // 等待线程汇入主线程
    SDL_WaitThread(thread1, NULL);
    SDL_WaitThread(thread2, NULL);
    printf("n is value: %d\n", n);

    /// 释放锁资源 ///
    SDL_DestroyMutex(mutex);
    /

    return 0;
}

条件变量使用

cpp 复制代码
#include <SDL.h>
#include <stdio.h>

SDL_mutex* g_mutex = NULL;  // 互斥锁
SDL_cond*  g_cond = NULL;   // 条件变量

// 线程等待函数
int thread_waitFunc(void* arg)
{
    printf("thread %d wait......\n", *(int*)arg);
    SDL_LockMutex(g_mutex);
    SDL_CondWait(g_cond, g_mutex);
    SDL_UnlockMutex(g_mutex);
    printf("thread wait end, start work......\n");

    return 1;
}

// 线程发送信号函数
int thread_signFunc(void* arg)
{
    printf("thread %d sleep 5s\n", *(int*)arg);
    sleep(5);                   // 5秒后唤醒等待的线程
    SDL_CondSignal(g_cond);     // 唤醒等待的线程
    printf("thread %d send signal success\n", *(int*)arg);
    return 666;
}

#undef main
int main()
{
    // 创建互斥锁和条件变量
    g_mutex = SDL_CreateMutex();
    g_cond  = SDL_CreateCond();
    if(g_mutex == NULL || g_cond == NULL)
    {
        printf("Create mutex or cond failed: %s\n", SDL_GetError());
        return -1;
    }
    // 创建线程
    int wait_num = 1001;
    SDL_Thread* thread_wait = SDL_CreateThread(thread_waitFunc, "threadWait", &wait_num);
    if(NULL == thread_wait)
    {
        printf("create threadWait error: %s\n", SDL_GetError());
        return -1;
    }
    wait_num++;
    SDL_Thread* thread_sign = SDL_CreateThread(thread_signFunc, "threadSignal", &wait_num);
    if(NULL == thread_sign)
    {
        printf("create threadSign error: %s\n", SDL_GetError());
        SDL_CondSignal(g_cond);
        SDL_WaitThread(thread_wait, NULL);
        return -1;
    }

    // 将两个线程汇入主线程
    SDL_WaitThread(thread_wait, NULL);
    SDL_WaitThread(thread_sign, NULL);

    // 释放锁跟条件变量的资源
    SDL_DestroyMutex(g_mutex);
    SDL_DestroyCond(g_cond);

    return 0;
}
相关推荐
EasyCVR10 小时前
EHOME视频平台EasyCVR视频融合平台使用OBS进行RTMP推流,WebRTC播放出现抖动、卡顿如何解决?
人工智能·算法·ffmpeg·音视频·webrtc·监控视频接入
简鹿办公11 小时前
使用 FFmpeg 进行音视频转换的相关命令行参数解释
ffmpeg·简鹿视频格式转换器·ffmpeg视频转换
EasyCVR15 小时前
萤石设备视频接入平台EasyCVR多品牌摄像机视频平台海康ehome平台(ISUP)接入EasyCVR不在线如何排查?
运维·服务器·网络·人工智能·ffmpeg·音视频
runing_an_min15 小时前
ffmpeg 视频滤镜:屏蔽边框杂色- fillborders
ffmpeg·音视频·fillborders
岁月小龙1 天前
如何让ffmpeg运行时从当前目录加载库,而不是从/lib64
ffmpeg·origin·ffprobe·rpath
行者记2 天前
ffmpeg命令——从wireshark包中的rtp包中分离h264
测试工具·ffmpeg·wireshark
EasyCVR2 天前
国标GB28181视频平台EasyCVR私有化视频平台工地防盗视频监控系统方案
运维·科技·ffmpeg·音视频·1024程序员节·监控视频接入
hypoqqq2 天前
使用ffmpeg播放rtsp视频流
ffmpeg
cuijiecheng20182 天前
音视频入门基础:FLV专题(24)——FFmpeg源码中,获取FLV文件视频信息的实现
ffmpeg·音视频
QMCY_jason2 天前
黑豹X2 armbian 编译rkmpp ffmpeg 实现CPU视频转码
ffmpeg