new is not suitable for multi threading?

Started by
10 comments, last by The C modest god 19 years, 1 month ago
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?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
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.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
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
CheersChris
You should also look at using _beginthreadex rather than CreateThread to create threads, if you are using standard library functions.
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]
It helps, because "there are some problems involved with using CRT functions in threads created with CreateThread()".

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

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]
I have used _beginthreadex and it says undeclared identifer.
I have included process.h, why its not working?
I have also tried to link LIBCMT.LIB and it doesnt help
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
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.
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?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.

This topic is closed to new replies.

Advertisement