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

news/2024/7/10 21:12:35 标签: ffmpeg

 事件使用

#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;
}

线程使用

#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;
}

 互斥锁使用

#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;
}

条件变量使用 

#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;
}

 


http://www.niftyadmin.cn/n/5134210.html

相关文章

校园物业报修小程序开发笔记一

背景 校园规模和复杂性&#xff1a; 大型学校和校园通常拥有众多的建筑物、设施和设备&#xff0c;需要有效的维护和报修系统&#xff0c;以满足学生、教职员工和校园管理人员的需求。 学生和员工需求&#xff1a; 学生和员工在校园内可能遇到各种维修问题&#xff0c;如故障的…

【工具】FreePic2PDF+PdgCntEditor|PDF批量添加书签(Windows)

这俩软件都不大&#xff0c;比较便携。 FreePic2PDF&#xff1a; 我下载的来源&#xff1a;https://www.52pojie.cn/thread-1317140-1-1.html&#xff08;包含下载链接https://www.lanzoui.com/it4x6j4hbvc&#xff09;下载的结果&#xff1a;https://pan.baidu.com/s/1r8n5G42…

大数据Flink(一百零五):SQL性能调优

文章目录 SQL性能调优 一、 ​​​​​​​MiniBatch 聚合

计算机毕业设计选题推荐-二手交易微信小程序/安卓APP-项目实战

✨作者主页&#xff1a;IT研究室✨ 个人简介&#xff1a;曾从事计算机专业培训教学&#xff0c;擅长Java、Python、微信小程序、Golang、安卓Android等项目实战。接项目定制开发、代码讲解、答辩教学、文档编写、降重等。 ☑文末获取源码☑ 精彩专栏推荐⬇⬇⬇ Java项目 Python…

GORM GEN 生成代码如何自定义方法和表名

GORM GEN: 是安全&友好的go orm 框架&#xff0c;基于gorm&#xff0c;支持多种数据库&#xff0c;mysql、sqlserver、postgres以及clickhouse等&#xff0c;通过代码生成的方式&#xff0c;生成安全&友好的orm代码&#xff0c;同时也支持类似java mybatis的 动态sql代…

Rust逆向学习 (3)

文章目录 Reverse for Shadow0x01. 变量类型不修改0x02. 变量类型修改 Reverse for ArrayReverse for MovingReverse for References and BorrowsReverse for String Slices总结 在本文中&#xff0c;我们将跟随《Rust权威指南》的学习路线&#xff0c;继续进行Rust逆向的学习。…

【技能树笔记】网络篇——练习题解析(十)

【技能树笔记】网络篇系列前九篇 【技能树笔记】网络篇——练习题解析&#xff08;一&#xff09;-CSDN博客 【技能树笔记】网络篇——练习题解析&#xff08;二&#xff09;-CSDN博客 【技能树笔记】网络篇——练习题解析&#xff08;三&#xff09;-CSDN博客 【技能树笔记】网…

Linux命令(108)之dirname

linux命令之dirname 1.dirname介绍 linux命令dirname是用来获取文件的指定路径 2.dirname用法 dirname [参数] NAME dirname参数 参数说明-z使用NUL而不是换行符分隔输出--help查看帮助信息--version查看版本信息 3.实例 3.1.获取文件的指定路径 命令&#xff1a; dirn…