Jump to content
  • Advertisement
Sign in to follow this  
The C modest god

new is not suitable for multi threading?

This topic is 4856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I get a userbreakpoint when running the following code:
struct TransAnimationReadBufferData{
//	StreamList<TransAnimationReadImageData> ImagesData;
	TransAnimationReadImageData * ImagesData;
	DWORD AvailableImageBuffer;
	ifstream * pFile;
	DWORD ImagesAmount;
};




DWORD 
WINAPI AnimationARRReadFileIntoRawBuffer(LPVOID lpParameter)
{
	DWORD counter;
	TransAnimationReadBufferData * pData;

	pData = static_cast<TransAnimationReadBufferData *>(lpParameter);
	for (counter=0; counter<pData->ImagesAmount/*pData->ImagesData.GetObjectsAmount()*/; counter++)
	{
		LPBYTE maintain;
		maintain = new BYTE [640*480];
//		ReadTransImageAdvancedRepresentationReduceFormatIntoRawBuffer (*pData->pFile, pData->ImagesData[counter]);
		pData->AvailableImageBuffer++;
	}
	return 0;
}


void 
TransAnimationSource::ReadAdvancedRepresentationReduceFormat (ifstream & File)
{
	DWORD counter;
	TransAnimationReadBufferData BufData;
	HANDLE ThreadHandle;
	DWORD ThreadID;

	FileIO::Read (File, this->FPS);
	FileIO::Read (File, counter);
	this->SetImagesAmount (counter);

	BufData.AvailableImageBuffer = 0;
//	BufData.ImagesData.Initialize (this->GetImagesAmount());
	BufData.ImagesData = new TransAnimationReadImageData [this->GetImagesAmount()];
	BufData.ImagesAmount = this->GetImagesAmount();
	BufData.pFile = &File;
	ThreadHandle = CreateThread(NULL, 0, AnimationARRReadFileIntoRawBuffer, static_cast<LPVOID>(&BufData), 0, &ThreadID);
	for (counter=0; counter<this->GetImagesAmount();)
	{
		if (counter<BufData.AvailableImageBuffer)
//		if (BufData.AvailableImageBuffer==this->GetImagesAmount())
		{
//		ReadTransImageAdvancedRepresentationReduceFormatIntoRawBuffer (File, BufData.ImagesData[counter]);
//			(*this)[counter].DecodeAdvancedRepresentationReduceFormat (BufData.ImagesData[counter]);
	LPBYTE maintain;
	maintain = new BYTE [480*640];
			counter++;
		}
	}
	CloseHandle (ThreadHandle);
}

It seems there is a problem calling new in both two threads. If I omit the new from either two of threads then everything works. Do you have any experience with this kind of problem? Do you know whats wrong?

Share this post


Link to post
Share on other sites
Advertisement
Decided to try this again have we? Very well. . .


{
// ...
LPBYTE maintain;
maintain = new BYTE [480*640];
counter++;
}

This screams memory leak to me.

You probably also want to wait for the first thread to finish before closing its handle. You can use WaitForSingleObject() to do that.

Lastly, if you have data that one thread can write to and another thread can read it, you need to make sure that the thread reading the data isn't doing it while the other thread is in the process of writing it. You can use a CriticalSection for that.

Share this post


Link to post
Share on other sites
also new is part of the c++std lib. make sure you link with the multithreaded version of the lib or it won't like threads.

Cheers
Chris

Share this post


Link to post
Share on other sites
Quote:
Original post by bakery2k1
You should also look at using _beginthreadex rather than CreateThread to create threads, if you are using standard library functions.


How does that help?

If you're going to tell him to use different functions, point him to Boost.Threads, which simply rocks (it's even typesafe!!). Example:

#include <iostream>
#include <boost/thread.hpp>
#include <boost/bind.hpp>

void print_hello_to( std::ostream * os )
{
*os << "Hello!!" << std::endl;
}

int main ( void )
{
boost::thread my_thread( boost::bind( print_hello_to , &std::cout ) );
my_thread.join();
}


edit: figured out why it didn't work with references - bind by default keeps arguments by VALUE instead of reference. There's a tool with just this problem in mind however:

#include <iostream>
#include <boost/thread.hpp>
#include <boost/bind.hpp>

void print_hello_to( std::ostream & os )
{
os << "Hello!!" << std::endl;
}

int main ( void )
{
boost::thread my_thread( boost::bind( print_hello_to , boost::ref( std::cout ) ) );
my_thread.join();
}


[Edited by - MaulingMonkey on March 7, 2005 3:11:34 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by bakery2k1
It helps, because "there are some problems involved with using CRT functions in threads created with CreateThread()".


Good to know.

Quote:
On another note, boost::thread does look nice - I may well be using that in the near future.


It is. The one thing that had me temporarily confused was the fact that creating new threads takes a parameter of type boost::function0< void >, making me wonder how I was supposed to send data to my thread. Then when I realized that the boost::function bit meant I could use functors, or boost::bind, and it all made sense :). Note I had to edit my example - for some reason it didn't like the reference to an ostream (odd, I used a reference in my code and no borkage...)

[Edited by - MaulingMonkey on March 3, 2005 7:33:30 PM]

Share this post


Link to post
Share on other sites
Make sure you are using the multithreaded C run-time library. _beginthreadex doesn't make any sense in asingle threaded program, so isn't defined in the single threaded version of the libraries.

Share this post


Link to post
Share on other sites
Quote:
Original post by bakery2k1
Make sure you are using the multithreaded C run-time library. _beginthreadex doesn't make any sense in asingle threaded program, so isn't defined in the single threaded version of the libraries.

How do I use the multithreaded library?
Do you mean linking to LIBCMT.LIB or MSVCRT.LIB?
Because I tried them both.
Is there anything else I am suppose to do?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!