简体   繁体   English

如何在DirectX中创建加载程序线程

[英]How to create a loader thread in directx

I have a game engine which used Directx 9 for rendering. 我有一个使用Directx 9进行渲染的游戏引擎。 I would like to be able to load sprite graphics in whilst the main update and render loop executes. 我希望能够在执行主要更新和渲染循环时加载Sprite图形。 Currently the engine has one main update and render loop so any loading done in this will pause the main loop whilst the graphics load. 当前,引擎具有一个主更新和渲染循环,因此在此过程中进行的任何加载都会在图形加载时暂停主循环。

I was looking at POSIX threads to do this. 我一直在寻找POSIX线程来做到这一点。 I have created the thread function and included mutex locks but the code crashes when its ran. 我已经创建了线程函数并包括了互斥锁,但是代码在运行时崩溃了。

Here is the thread function: 这是线程函数:

void GameApp::InternalThreadEntry()
{
    pthread_mutex_lock (&mutex);

    for(int i = 0; i < MAX_NUMBER; i++)
    {
        test_loader_sprites[i].loadImage(window1,"Test_Image.tga");
    }

    has_finishd_loading = true;

    pthread_mutex_unlock (&mutex);
}

The Code crashes in my engines render function. 代码在我的引擎渲染函数中崩溃。 Im sure this is because the directx device, which is a member of the window1 instance, is accessed for loading by the thread whilst the main application accesses it for rendering. 我肯定这是因为线程访问了window1实例成员的directx设备以进行加载,而主应用程序则访问了该设备以进行渲染。

Could you shed a little light on where im going wrong. 你能告诉我即时错误在哪里。 Im new to using threads. 我是新来使用线程。

All the best, Martin 祝一切顺利,马丁

Threading is often a wolf in sheeps clothing. 穿线通常是披着羊皮的狼。 It looks to solve so many problems, but in reality can cause so many more than they solve. 它看起来可以解决很多问题,但实际上造成的问题远远超过了解决的问题。 Fortunately you have what many would believe a valid scenario to use an additional thread. 幸运的是,您有许多人相信使用附加线程的有效方案。

Having written a similar loader myself a while back, your problem looks to indeed be that you are accessing Window1 while its in use elsewhere. 前一段时间我自己编写了一个类似的加载器,您的问题似乎确实是您在访问Window1时正在其他地方使用它。 Basically, does your main thread do anything with Window1 while the thread would be running. 基本上,您的主线程在Window1运行时是否对Window1做任何事情。 If so then that would indicate it to be the problem. 如果是这样,那将表明这是问题所在。

The solution I found was to properly separate out data storage from the renderer. 我发现的解决方案是正确地将数据存储与渲染器分离。 You can load in the data to a store in a thread. 您可以将数据加载到线程中的存储中。 Once that has done, pass that information in the store to the main thread at the end of the thread (or move it directly in the main thread later). 完成此操作后,将存储区中的信息传递到线程末尾的主线程(或稍后将其直接移入主线程)。 This avoids dependency on your Window1 object within the thread. 这样可以避免依赖线程内的Window1对象。

It would be a good idea to do some further reading on threads before embarking on a larger scale scenario it sounds you are working on. 在开始听起来正在努力的更大规模的场景之前,最好对线程做一些进一步的阅读。 Working with threads is one of the more fiddly and complicated areas of software design, and from experience you can't learn them well enough. 使用线程是软件设计中比较棘手和复杂的领域之一,并且从经验中您可能无法充分了解它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM