Hello!
I have a problem with Boost threads. The program is a "for fun" program that draws the Mandelbrot set in DirectX 11. I want to use threads to be able to make use of a progress bar (or circle in this case), as it takes around 30-40 seconds to calculate the image at full HD on this computer. To calculate the image, I have a Mandelbrot class with a method "Calculate", which fires up 4 threads that split the calculation into 4 parts of the image. If I just instantiate an object of the Mandelbrot class, and call the Calculate-method, all works. However, I cannot use the progress circle this way. To do that, I want to fire up another thread, which has the Calculate method as starting point, and let the main thread go on with the progress circle business. This works sometimes, but most of the time, the application crashes and burns. Badly. Seems random when it crashes as well. Anybody have boost experience, perhaps in combination with DirectX 11?
I think I'm correct in that device context calls are not threadsafe, why I have arranged for a mutex to lock every time the device context is used. This should not be needed for the Direct3D Device, right?
If it crashes (which is most of the times I run it), only two of the calculate-threads terminate. The other two + the "CalcThread" do not finish. Any ideas?
Some code.
Dont worry about the making of progress circle objects. It's extremely inefficient. I know. But it should have nothing to do with the issue.
[source lang="cpp"]void MandelbrotMain::InitializeObjects() {
Shader* unlitTextureShader = new Shader(d3dEngine, UNLIT_TEXTURESHADER);
unlitTextureShader->Initialize();
shaders["UNLIT_TEXTURESHADER"] = unlitTextureShader;
pMandelbrot = new Mandelbrot(d3dEngine, shaders["UNLIT_TEXTURESHADER"]);
//pMandelbrot->Calculate();
boost::thread* t1 = new boost::thread(boost::bind(&Mandelbrot::Calculate, pMandelbrot));
OutputDebugString(L"CalcThread starting: "); OutputDebugStringA(ToString(t1->get_id()).c_str());
}
void MandelbrotMain::GameLoop() {
MSG msg;
while (true) {
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT) {
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else {
/* RENDER THE SCENE */
d3dEngine->BeginScene(XMFLOAT4(0.0f, 0.0f, 0.0f, 0.0f));
Sprite* pSprite;
if (pMandelbrot->GetDone()) {
pSprite = pMandelbrot->GetSprite();
pSprite->ResetTransforms();
pSprite->Scale(700.0f, 400.0f, 1.0f);
pSprite->Render();
}
else {
if (pProgress) {
delete pProgress;
pProgress = 0;
}
pProgress = new Progress(d3dEngine, shaders["UNLIT_TEXTURESHADER"], pMandelbrot->GetPercentDone());
pSprite = pProgress->GetSprite();
pSprite->ResetTransforms();
pSprite->Scale(100.0f, 100.0f, 1.0f);
if (!pMandelbrot->GetDone()) {
pSprite->Render();
}
}
if (pProgress) {
delete pProgress;
pProgress = 0;
}
d3dEngine->EndScene();
/* END OF RENDERING */
}
}
}
[/source]
[source lang="cpp"]void Mandelbrot::Calculate() {
boost::thread t1(boost::bind(&Mandelbrot::CalculateMandelbrotRange, this, _1, _2), -1.0f, -0.50000001f);
OutputDebugString(L"\nThread 1 starting: "); OutputDebugStringA(ToString(t1.get_id()).c_str());
boost::thread t2(boost::bind(&Mandelbrot::CalculateMandelbrotRange, this, _1, _2), -0.5f, -0.00000001f);
OutputDebugString(L"\nThread 2 starting: "); OutputDebugStringA(ToString(t2.get_id()).c_str());
boost::thread t3(boost::bind(&Mandelbrot::CalculateMandelbrotRange, this, _1, _2), 0.0f, 0.49999999f);
OutputDebugString(L"\nThread 3 starting: "); OutputDebugStringA(ToString(t3.get_id()).c_str());
boost::thread t4(boost::bind(&Mandelbrot::CalculateMandelbrotRange, this, _1, _2), 0.5f, 0.99999999f);
OutputDebugString(L"\nThread 4 starting: "); OutputDebugStringA(ToString(t4.get_id()).c_str());
OutputDebugString(L"\n");
t1.join();
t2.join();
t3.join();
t4.join();
pSprite = new Sprite(pD3DEngine, pShader);
pSprite->Initialize(0, pixels, xw, yw, xw*16); //12 = 4 bytes per color, 3 colors (RGB), xw*12 is number of bytes per row
delete [] pixels;
OutputDebugString(L"Done, let's render\n");
done = true;
}
[/source]
Boost threaded application crashes
When you say 'crash' - what exactly is happenning? I.E. is it an access violation or what?
FWIW, it's not really worth doing your own threading for paralellism - it's simpler and more scaleable to use Intel Threaded Building Blocks or Microsoft Parallel Patterns Library.
Not that you have to of course - post more info and we'll try to help.
FWIW, it's not really worth doing your own threading for paralellism - it's simpler and more scaleable to use Intel Threaded Building Blocks or Microsoft Parallel Patterns Library.
Not that you have to of course - post more info and we'll try to help.
I get an unhandled exception. It doesnt really give me much more information that this. I have only worked on this on my laptop. On my more powerful desktop, I have run it about 10 times in a row without crashes now. Starting to think it's just as simple as an "out of memory" issue. Could it be this?
Hard to tell, but possible - paste the entirety of the message you get, please
An unhandled exception could be a real c++ exception, or more likely SEH reporting an access violation.
An unhandled exception could be a real c++ exception, or more likely SEH reporting an access violation.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement