new is not suitable for multi threading?

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

Recommended Posts

I get a userbreakpoint when running the following code:
struct TransAnimationReadBufferData{
DWORD AvailableImageBuffer;
ifstream * pFile;
DWORD ImagesAmount;
};

DWORD
{
DWORD counter;

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

void
{
DWORD counter;

this->SetImagesAmount (counter);

BufData.AvailableImageBuffer = 0;
//	BufData.ImagesData.Initialize (this->GetImagesAmount());
BufData.ImagesAmount = this->GetImagesAmount();
BufData.pFile = &File;
for (counter=0; counter<this->GetImagesAmount();)
{
if (counter<BufData.AvailableImageBuffer)
//		if (BufData.AvailableImageBuffer==this->GetImagesAmount())
{
LPBYTE maintain;
maintain = new BYTE [480*640];
counter++;
}
}
}


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 on other sites
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 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 on other sites
You should also look at using _beginthreadex rather than CreateThread to create threads, if you are using standard library functions.

Share on other sites
Quote:
 Original post by bakery2k1You 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 on other sites
Quote:
 Original post by bakery2k1It 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 on other sites
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

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 on other sites
Quote:
 Original post by bakery2k1Make 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?

1. 1
2. 2
Rutin
21
3. 3
A4L
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 44
• Forum Statistics

• Total Topics
633741
• Total Posts
3013624
×