[C++]Strange issue related to multithreading

Started by
5 comments, last by Hodgman 11 years, 8 months ago
Hello,

Firstly, I'd like to say sorry if this is wrong forum, as issue might be related to Visual Studio.

I am using Win8 x64, Visual Studio 2012, DirectX 11 with feature level 10.0. I have 2 threads: main thread and rendering thread. Main thread initializes window, DirectX and starts rendering thread. Rendering thread is simple:

while(true) {
for(auto &entity : renderList) {
//...
}
present();
}

While all resource loading is handled by main thread.

When I run the program it works fine, however when I attach Visual Studio's Graphics Debugger calling IASetIndexBuffer causes crash with similar debug output:

D3D11 CORRUPTION: ID3D11DeviceContext::IASetIndexBuffer: First parameter is corrupt. [ MISCELLANEOUS CORRUPTION #13: CORRUPTED_PARAMETER1]
First-chance exception at 0x77037945 (KernelBase.dll) in FxdTmStpTest.exe: 0x0000087D (parameters: 0x00000000, 0x04EEDB64, 0x04EECF9C).
Unhandled exception at 0x77037945 (KernelBase.dll) in FxdTmStpTest.exe: 0x0000087D (parameters: 0x00000000, 0x04EEDB64, 0x04EECF9C).

I suspect it happens because I create that index buffer on other thread than it's used at.

This issue happens on Debug x86, Debug x64 builds, however Release x64 build has no issues (I am unable to try Release x86 because of unaligned SIMD data).

If anyone has any clues what's wrong please let me know. Thank you in advance.
Advertisement
I also do some multithreaded loading and the graphics debugger crashes on it, perhaps a bug in the debugger itself, looks like it doesn't support multithreading
the debuglayer allways work for me when i am using multithreading.

when it comes to multithreading, make sure you never let more than one thread read/write at the same time.
Even if this sounds like "that would never happen", assume it will!

that might not fix your problem, but it´s a start to work with.

it could aswell be that you have created some of your buffer wrong, or that you are trying to use them in a invalid way.
"There will be major features. none to be thought of yet"
How do you synchronise and transfer data between your two threads?
I am unsure if I need any synchronization between ID3D11Device and ID3D11DeviceContext as they're on different threads. Right now I have no synchronization between them, could that be an issue? I thought DirectX synchronizes it automatically, since there is flag related to singlethreading (D3D11_CREATE_DEVICE_SINGLETHREADED), which I'm not using.

As for data between threads, I use concurrency::concurrent_vector from <concurrent_vector.h>.
Roughly my code looks like this:

// Scene class's field
concurrency::concurrent_vector<Entity*> entities;

// Scene::createEntity(). This is executed on Main Thread (thread1)
Entity *entity = new Entity();
entity->indexBuffer = DirectX::createIndexBuffer(...);
entity->vertexBuffer = DirectX::createVertexBuffer(...);
entities.push_back(entity);

// This is executed on Rendering Thread (thread2)
for(auto &entity : entities) {
// any entities here will have index and vertex buffer created
// unless they're not actually created right away and I have to wait till some event
}


Note: As I said in original post, it crashes only when I'm using Visual Studio's Graphical Debugger. Running it normally or in Debug mode doesn't cause any crashes or warning in the output.
if you have main thread that alters data that render thread uses, BE AWARE. google "lock" key word. In other words, change memory of thread A only in thread A. Else you will have to synchronize.
I've not used [font=courier new,courier,monospace]concurrent_vector[/font], but assuming you're not editing the entities after creation, that looks safe. I'm stumped.
How does the "unless they're not actually created right away and I have to wait till some event" bit work?

This topic is closed to new replies.

Advertisement