473,569 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

new and delete from different threads

I am aware that the C++ standard in its present form does not say
anything about threads, however I do have a relevant question.

I am working on Windows XP/VC++ 8.0.

Is there a problem new'ing a bunch of objects from one thread and
deleting them in another? I do something like:

struct GenericPointerD eleter
{
template<typena me T>
void operator()(T* p)
{
if (ptr != 0)
{
delete ptr;
ptr = 0;
}
}
};

typedef vector<classofp ointers*> vecptrs;
int main()
{
vecptrs myptrs;
CreateThread(.. .... (LPVOID)&myptrs ........);
// use some platform specific API to wait until
threadcallbackr outine terminates
pseudo_wait_for _terminate(thre adcallbackrouti ne);

for_each(myptrs .begin(), myptrs.end(), GenericPointerD eleter());
}

DWORD WINAPI threadcallbackr outine(LPVOID param)
{
vecptrs* my_ptrs = static_cast<vec ptrs*>(param);
my_ptrs->push_back(ne w classofpointers ());
return 0;
}

I have vastly simplified what is essentially happening in my
application...

Should I be careful about new'ing and deleting from the same thread?

May 10 '06 #1
15 8217

Dilip wrote:
I am aware that the C++ standard in its present form does not say
anything about threads, however I do have a relevant question.

What makes your question on topic? I don't see anything other than a
question about threading, which is not on topic since this newsgroup
only covers standard C++.
I am working on Windows XP/VC++ 8.0.
Yes, but that's irrelevant on this newsgroup.
Should I be careful about new'ing and deleting from the same thread?


If you do new and delete in the same thread ( in a multi-threaded
application ), how is that any different than doing new and delete in a
single threaded application?

May 10 '06 #2
Dilip wrote:
....
Is there a problem new'ing a bunch of objects from one thread and
deleting them in another? ...


There should be no problem doing that on Win32, Linux, MAC or many other
unicies I've worked on.

For some versions of malloc, it will be somewhat less efficient but
unless performance is a big issue, then you should have no worries.
May 10 '06 #3
In article <11************ *********@u72g2 000cwu.googlegr oups.com>,
"Dilip" <rd*****@lycos. com> wrote:
I am aware that the C++ standard in its present form does not say
anything about threads, however I do have a relevant question.

I am working on Windows XP/VC++ 8.0.

Is there a problem new'ing a bunch of objects from one thread and
deleting them in another? I do something like:

struct GenericPointerD eleter
{
template<typena me T>
void operator()(T* p)
{
if (ptr != 0)
{
delete ptr;
ptr = 0;
}
}
};

typedef vector<classofp ointers*> vecptrs;
int main()
{
vecptrs myptrs;
CreateThread(.. .... (LPVOID)&myptrs ........);
// use some platform specific API to wait until
threadcallbackr outine terminates
pseudo_wait_for _terminate(thre adcallbackrouti ne);

for_each(myptrs .begin(), myptrs.end(), GenericPointerD eleter());
}

DWORD WINAPI threadcallbackr outine(LPVOID param)
{
vecptrs* my_ptrs = static_cast<vec ptrs*>(param);
my_ptrs->push_back(ne w classofpointers ());
return 0;
}

I have vastly simplified what is essentially happening in my
application...

Should I be careful about new'ing and deleting from the same thread?


I don't have access to Windows, but I believe your code is fine. The
C++ committee is interested in C++0X have a multi-threaded memory model
which will probably be based on current practice today. And as far as I
know, current practice is that transferring memory ownership across
thread boundaries is fine.

-Howard
May 10 '06 #4
Dilip wrote:
I am aware that the C++ standard in its present form does not say
anything about threads, however I do have a relevant question.
You could frame it in terms of Boost.Threads, since something along
those lines will likely be included in C++09. Then it would be on
topic. :-)

I am working on Windows XP/VC++ 8.0.

Is there a problem new'ing a bunch of objects from one thread and
deleting them in another? I do something like:

struct GenericPointerD eleter
{
template<typena me T>
void operator()(T* p)
{
if (ptr != 0)
{
delete ptr;
ptr = 0;
}
}
};
First, there's no need to check for null before deleting. Second,
zeroing the pointer is generally unnecessary, though it might be useful
if you reuse the pointer (doesn't look like you do below) but such
reuse is often considered bad practice. Lastly, you're duplicating
existing functionality. You could use std::tr1::share d_ptr (aka
boost::shared_p tr) for a smart pointer that works with standard
containers.

typedef vector<classofp ointers*> vecptrs;
If you do this...

typedef vector< shared_ptr<clas sofpointers> > vecptrs;
int main()
{
vecptrs myptrs;
CreateThread(.. .... (LPVOID)&myptrs ........);
// use some platform specific API to wait until
threadcallbackr outine terminates
pseudo_wait_for _terminate(thre adcallbackrouti ne);

for_each(myptrs .begin(), myptrs.end(), GenericPointerD eleter());
....then this is completely unnecessary. It is done automagically by the
destructors.
}

DWORD WINAPI threadcallbackr outine(LPVOID param)
{
vecptrs* my_ptrs = static_cast<vec ptrs*>(param);
my_ptrs->push_back(ne w classofpointers ());
return 0;
}

I have vastly simplified what is essentially happening in my
application...

Should I be careful about new'ing and deleting from the same thread?


That could be platform specific. <OT>But in your case it probably
doesn't matter.</OT>

Cheers! --M

May 10 '06 #5

Thanks to everyone that replied! Much appreciated. Now I can safely
look for my out of memory errors elsewhere.

I have some comments inline.

mlimber wrote:
Dilip wrote: First, there's no need to check for null before deleting.
I know.. more like defensive coding. :)
Second,
zeroing the pointer is generally unnecessary, though it might be useful
if you reuse the pointer (doesn't look like you do below) but such
reuse is often considered bad practice. Lastly, you're duplicating
existing functionality. You could use std::tr1::share d_ptr (aka
boost::shared_p tr) for a smart pointer that works with standard
containers.

If you do this...

typedef vector< shared_ptr<clas sofpointers> > vecptrs;
for_each(myptrs .begin(), myptrs.end(), GenericPointerD eleter());


...then this is completely unnecessary. It is done automagically by the
destructors.


I would like nothing better than using Boost but management has a
policy on open-source projects and one of them involves being paranoid
about downloading them. :-) NIH syndrome is very prevalent around here.

May 10 '06 #6
Dilip wrote:
First, there's no need to check for null before deleting.
I know.. more like defensive coding. :)


The FAQ calls it "wrong":

http://www.parashift.com/c++-faq-lit....html#faq-16.8
I would like nothing better than using Boost but management has a
policy on open-source projects and one of them involves being paranoid
about downloading them. :-) NIH syndrome is very prevalent around here.


Do they let you use the standard library? You might try to make the
case that shared_ptr is part of the standard C++ library working
group's technical report -- (currently) non-normative extensions to the
C++ standard library that will likely be included in the next version
of the library -- not just a run-of-the-mill open-source library.

Cheers! --M

May 10 '06 #7
In article <11************ *********@j73g2 000cwa.googlegr oups.com>,
"mlimber" <ml*****@gmail. com> wrote:
You might try to make the
case that shared_ptr is part of the standard C++ library working
group's technical report -- (currently) non-normative extensions to the
C++ standard library that will likely be included in the next version
of the library


News flash: shared_ptr was voted into the C++0X working draft on April
7, 2006. :-)

I'm not meaning to contradict you. It is still non-normative as you
say. Just thought this to be an important update.

-Howard
May 10 '06 #8
Dilip wrote:
I would like nothing better than using Boost but management has a
policy on open-source projects and one of them involves being paranoid
about downloading them. :-) NIH syndrome is very prevalent around here.


shared_ptr is a de-facto standard, and soon-to-be official standard. The
Boost library is not distributed under the GPL, it has a very strict
policy to keep the code freely usable in commercial code. The library
strives for a very high quality, and many of the contributors are among
the most respected members of the C++ community.

shared_ptr is probably the single most valuable part of Boost. It truly
increases the stability of your code, not only by automatically cleaning
up resources, but also by virtually eliminating accidental double
deletions and dereferencing dead or uninitialized pointers. Using
weak_ptr you can ensure that your weak references automatically expire
when the last copy of the owned object they point to goes out of scope.

I've written an introductory tutorial about shared_ptr, which should get
you started:

http://tweakbits.com/articles/sharedptr/index.html

Tom
May 10 '06 #9

Tamas Demjen wrote:
Dilip wrote:
shared_ptr is probably the single most valuable part of Boost. It truly
increases the stability of your code, not only by automatically cleaning
up resources, but also by virtually eliminating accidental double
deletions and dereferencing dead or uninitialized pointers. Using
weak_ptr you can ensure that your weak references automatically expire
when the last copy of the owned object they point to goes out of scope.


Would you know whether or not shared_ptr will be thread-safe?

May 10 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
3155
by: Christopher Benson-Manica | last post by:
Is the following code legal, moral, and advisable? #include <iostream> class A { private: int a; public: A() : a(42) {}
0
1135
by: Raghu Rudra | last post by:
I have an ASP.NET web application, which accepts posted xml to do its work and returns response xml. This xml is large and is in the order of 1MB. I started testing this web app with client utility which spawns multiple threads to post xml to this web site. I noticed an interesting thing. When I kept the client utility and the web app on the...
10
1456
by: n2xssvv g02gfr12930 | last post by:
In a job interview I was asked about the statement below: delete this; Naturally I was horrified, yet they claimed they had used it. Personally I'm pretty damn sure I could never justify this. So is this a case of Lord Bryon, "Mad, bad, and dangerous to know", or do you think you could ever justify using it? JB
1
20205
by: peter.moss | last post by:
I have an application that use log4net for operational logging. I store some user data elsewhere and need to tie the two together. To achieve this I pass the ThreadId across on the user table so I can see what thread the user was running under and then look in the log4net table to see what they were up to. This works fine when I use...
19
2587
by: Xavier Décoret | last post by:
Hi, I long thought one could not delete a const pointer but the following code compiles under g++ and visual net: class Dummy { public: Dummy() {} ~Dummy() {}
6
2184
by: ismail69 | last post by:
I want to delete bunch of records from a table like, delete from Table1 where col1 in (select col1 from Table2); now col1 from Table2 will be some where b/w 200-500. But Table1 will be having 3000-5000 records for a single entry from Table2. Hence instead of deleting at a stretch, I want to get the col1 from Table2 in some Java...
0
2944
by: Jean-Paul Calderone | last post by:
On Sat, 23 Aug 2008 02:25:17 +0800, Leo Jay <python.leojay@gmail.comwrote: No - it's just what I said. create_socket creates one socket and passes it to read_socket and write_socket. read_socket calls connect on the socket it is passed. write_socket calls accept on the socket it is passed. So a single socket has connect and accept called...
13
1852
by: mac | last post by:
hi all, im creating a form wich wil upload images to a folder and their names and other details to a database. im able to do uploading but my delete function is not working, please can anybody tell me mistake n help me?? image_uploader.php <? if(isset($_REQUEST)) {
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7681
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6290
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
950
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.