|
I am currently developing a multi-threaded application using C++ and MFC. Each thread calls new and delete to allocate and free memory. I am wondering if this is ok. In particular, if I have a routine that does the following, is it thread safe?
<br />
void<br />
WorkerClass::genRandomNumbers( double *randomNumbers, int count )<br />
{ <br />
double *ranList = new double[6];<br />
<br />
<br />
delete []ranList;<br />
}<br />
I am thinking that if two different threads are executing this routine at the same time, one might be allocating memory and the other freeing memory. Is that ok? One possibility is that Microsoft has put locks in there code to make sure that new and delete are thread safe. I have been unable to find documentation saying that they have or have not done this. I am hoping somebody out there can enlighten me on this issue.
Thanks
Bob
|
|
|
|
|
BobInNJ wrote: I am thinking that
My advice is not to use your own thinking about how multi-threading works. I strongly urge you to find some material designed to explain or teach multi-threading concepts and study it.
If this is something you decide to do, I can tell you that back when was starting out with threading I found this book[^] (the chapters on threading) very helpful. It was the first edition and I can't verify for you that the new editions contain the same material.
|
|
|
|
|
Perfectly safe. Unless you allocate something static. like
void
WorkerClass::genRandomNumbers( double *randomNumbers, int count )
{
static int i =0;
i = blahaha
}
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
|
|
|
|
|
VuNic wrote: Perfectly safe
Really? Even though you have no idea what is happening in the "details not shown"?
|
|
|
|
|
Oops! that was not to negate your message up there.. as you see it's all posted at the same time. "Perfectly-Safe" is perfectly restricted to that block of code he posted. Certainly I wanted to add a note asking him to look at the JR book you hrefed. but felt quite lazy to edit the text again
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
|
|
|
|
|
Automatic variables (like ranlist ) are thread safe.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Everyone has there own definition of "mean", that seems far meaner than any of the offensive things I post to people!
|
|
|
|
|
new will call malloc, which (eventually) calls HeapAlloc on the module CRT heap.
If you look at the Heap* API function docs you will see that they are thread safe by default (though this feature can be disabled) ... actually, to be precise, malloc will use a CRT lock (critical section) to lock the heap, but the effect is the same.
So yes, new and delete are thread safe.
As to your example; as led mike pointed out, it depends on what you are doing in 'Do some work, details not shown'.
...cmk
The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.
- John Carmack
|
|
|
|
|
I have an edit control in a CFormview dialog.
The edit control is DDXed to a some special class which is giving hyperlink functionality.
The control is disappearing or its background becomes same as the dialog sometime and when mouse cursor goes on top of it, it appears from then.
It happens mostly when setting text on that control (by function call) and
the control's class calls "RedrawWindow()", "InvalidateRect" & "UpdateWindow()" for the parent dialog(CFormView) of control when setting text on it.
The problem is happening in Win XP system and not in Vista.
The Formview is using "CreateSolidBrush()" and "FillRect()" function inside OnPaint() to change its back color.
Any suggstion would be greatly appreciable..
--
"Programming is an art that fights back!"
modified on Thursday, March 19, 2009 1:14 PM
|
|
|
|
|
Could it be that your edit is overlapped by some other control like a static for example?
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|
|
Running VS2008, writing an SDI in VC++. Here's the code in a nutshell:
float htmin;
CString value;
value=mslstr.Mid(parse_start, parse_stop-parse_start);
htmin=atof(value);
VS throws the C4244 warning at the htmin=atof(value); line.
Am I missing something stupid here?
|
|
|
|
|
Nothing. atof returns you a double, and you are assigning it to a float. Nothing serious.
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
|
|
|
|
|
Thx, stickler for no warnings. Is only remedy to initialize htmin as double?
|
|
|
|
|
penny black wrote: Is only remedy to initialize htmin as double?
An explicit cast will do the trick
htmin = static_cast<float>(atof(value));
BTW atof is just an axample of misnamed function.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
modified on Thursday, March 19, 2009 2:46 PM
|
|
|
|
|
another way to eat the warning
std::istringstream b("100.003133");
float f1;
b >> f1;
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
|
|
|
|
|
very good, indeed.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
static_cast gave me syntax error.
|
|
|
|
|
It shouldn't (I made a test).
What is exactly your code (and waht is the error message)?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Here is, what I think, the pertinent part of code:
CString value;
value=mslstr.Mid(parse_start, parse_stop-parse_start);
float htmin;
htmin=static_cast(atof(value));
Here is the error:
.\sruleDoc.cpp(1072) : error C2059: syntax error : '('
Line 1072 is htmin=static_cast(atof(value));
|
|
|
|
|
Should be
htmin=static_cast<float>(atof(value));
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
lol he mentioned a sample.. to be precise,
static_cast<float>(atof(value))
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
|
|
|
|
|
CPallini wrote: It shouldn't (I made a test).
But you probably forgot that the < and > characters are removed from your post
|
|
|
|
|
Oh, my bad . Thank you.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Sorry, I made a typo in my example. Fixed now. Sorry again.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
penny black wrote: Is only remedy to initialize htmin as double?
Or better would be to ignore it?
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
|
|
|
|