如何向SDL线程发送其他参数? [英] How to send additional parameters to an SDL Thread?

查看:233
本文介绍了如何向SDL线程发送其他参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我知道如何创建一个SDL线程。

Yes, I know how to create an SDL thread.

int myfunc(void* data)
{
    //my code...
}
SDL_CreateThread* mythread=SDL_CreateThread(myfunc,NULL);

但是如果我想做类似的操作呢?

But what if i want to do something like:

int myfunc(void* data,int myparameter1,char myparameter2)
{
    //my code...
}
SDL_CreateThread* mythread=SDL_CreateThread(myfunc,NULL,42,'c');

即如何为一个具有多个参数的函数创建一个线程* data')
如果这是不可能的,你可以建议任何方法,通过它我可以传递参数到线程?

i.e how to create a thread for a function with more than one parameters (parameters excluding the usual 'void* data') If this is not possible can you suggest any method by which i can pass a parameter to a thread?

推荐答案

您可以在堆上创建 struct ,使用您的数据设置其字段,然后将其地址传递给 SDL_CreateThread

You can create a struct on the heap, set its fields with your data, then pass its address to SDL_CreateThread:

typedef struct {
    int param1;
    char param2;
} ThreadData;

int myfunc(void* data)
{
    ThreadData *tdata = data;
    int param1 = tdata->param1;
    char param2 = tdata->param2;
    free(data); // depending on the content of `data`, this may have
                // to be something more than a single `free`
    //my code...
}
ThreadData *data = malloc(sizeof(ThreadData));
data->param1 = ...;
data->param2 = ...;
SDL_CreateThread* mythread=SDL_CreateThread(myfunc,data);

这篇关于如何向SDL线程发送其他参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆