Sign in to follow this  
Ripiz

[C++]Strange issue related to multithreading

Recommended Posts

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:
[CODE]
while(true) {
for(auto &entity : renderList) {
//...
}
present();
}
[/CODE]
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:
[CODE]
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).
[/CODE]
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 [i]concurrency::concurrent_vector[/i] from [i]<concurrent_vector.h>[/i].
Roughly my code looks like this:
[CODE]
// 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
}
[/CODE]

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. Edited by Ripiz

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 "[i]unless they're not actually created right away and I have to wait till some event[/i]" bit work?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this