473,386 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Thread question


Hi,

(me using visual c++ 2005, all native/unmanaged).
So my question is about allocating memory in one thread and using it in
another.
I think its good to explain as following :

Somewhere I have this:
someclass * _obj;

thread_proc1() //used by thread1
{
// this thread does the following:
_obj = new someclass();
.......
// thread exits, but does not delete _obj....
}

thread_proc2() // used by thread 2
{
/* this thread starts later on and plays around
with _obj object that was allocated in thread 1 */

}

I want to know is all this ok? I just had a confusion that bcuz the _obj
variable was declared in thread1 I may have some problem accessing it in
thread 2 after thread 1 is finished. But on the other hand I thought that
since _obj was allocated in the process heap, which wont deallocate when
thread1 exits, so it should be ok for thread 2. And thread 2 can even delete
it. Am I write in thinking of the process's heap this way?

Regards,

Ab.

Apr 20 '06 #1
3 1092
> (me using visual c++ 2005, all native/unmanaged).
So my question is about allocating memory in one thread and using it in
another.
I think its good to explain as following :

Somewhere I have this:
someclass * _obj;

thread_proc1() //used by thread1
{
// this thread does the following:
_obj = new someclass();
.......
// thread exits, but does not delete _obj....
}

thread_proc2() // used by thread 2
{
/* this thread starts later on and plays around
with _obj object that was allocated in thread 1 */

}

I want to know is all this ok? I just had a confusion that bcuz the _obj
variable was declared in thread1 I may have some problem accessing it in
thread 2 after thread 1 is finished. But on the other hand I thought that
since _obj was allocated in the process heap, which wont deallocate when
thread1 exits, so it should be ok for thread 2. And thread 2 can even delete
it. Am I write in thinking of the process's heap this way?


You are right in your assumptions, but programming like this is dangerous.
you have to think of the following scenarios:
- what happens if thread2 runs first?
- what happens if thread1 runs twice before thread2 runs?
- what happens if thread1 and thread2 are running at the same time?
- ...

if you wait with starting thread2 until thread1 has finished, the advantages
of multithreading are gone too.
even if you start thread2 after thread1, there is no guarantee that thread1
will be running when thread2 starts running. that depends on scheduling.

it would be better to new the object in your program initialization, and
then delete it after all threads have run.

What you do of course depends on what you want your application to do, but
assuming that things have happened in 1 thread before something else happens
in another thread is asking for hard to find runtime problems.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"
Apr 20 '06 #2
Abubakar wrote:
I want to know is all this ok? I just had a confusion that bcuz the _obj
variable was declared in thread1 I may have some problem accessing it in
thread 2 after thread 1 is finished. But on the other hand I thought that
since _obj was allocated in the process heap, which wont deallocate when
thread1 exits, so it should be ok for thread 2. And thread 2 can even delete
it. Am I write in thinking of the process's heap this way?


You are right about heap not caring about which thread creates and which
deletes an object. If there is a well known point in time before which
thread 1 stops (and after which thread 2 starts) caring about an object
then your code is safe.
Apr 20 '06 #3
Ok Thanks guys.

"Bruno van Dooren" <br**********************@hotmail.com> wrote in message
news:4B**********************************@microsof t.com...
(me using visual c++ 2005, all native/unmanaged).
So my question is about allocating memory in one thread and using it in
another.
I think its good to explain as following :

Somewhere I have this:
someclass * _obj;

thread_proc1() //used by thread1
{
// this thread does the following:
_obj = new someclass();
.......
// thread exits, but does not delete _obj....
}

thread_proc2() // used by thread 2
{
/* this thread starts later on and plays around
with _obj object that was allocated in thread 1 */

}

I want to know is all this ok? I just had a confusion that bcuz the _obj
variable was declared in thread1 I may have some problem accessing it in
thread 2 after thread 1 is finished. But on the other hand I thought that since _obj was allocated in the process heap, which wont deallocate when
thread1 exits, so it should be ok for thread 2. And thread 2 can even delete it. Am I write in thinking of the process's heap this way?
You are right in your assumptions, but programming like this is dangerous.
you have to think of the following scenarios:
- what happens if thread2 runs first?
- what happens if thread1 runs twice before thread2 runs?
- what happens if thread1 and thread2 are running at the same time?
- ...

if you wait with starting thread2 until thread1 has finished, the

advantages of multithreading are gone too.
even if you start thread2 after thread1, there is no guarantee that thread1 will be running when thread2 starts running. that depends on scheduling.

it would be better to new the object in your program initialization, and
then delete it after all threads have run.

What you do of course depends on what you want your application to do, but
assuming that things have happened in 1 thread before something else happens in another thread is asking for hard to find runtime problems.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"

Apr 20 '06 #4

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

Similar topics

9
by: rnn98 | last post by:
hi, my multithread application, running under solaris box, is crashing eventually. I tried to spot and substitute functions not "thread safe", but I guess my search wasn't good enough. I have put...
5
by: Bill Davidson | last post by:
Hello All: I've got a question about synchronization requiremements in a C# worker thread procedure that, among other things, sinks events from outside sources. I realize the worker thread will...
1
by: Bill Davidson | last post by:
(RESEND: I added a little more code to the sample for clarity) Hello All: I've got a question about synchronization requiremements in a C# worker thread procedure that, among other things,...
2
by: James Lavery | last post by:
Hi everyone, We're developing an application to capture data from several serial ports, store in a database, and (optionally) forward on using FTP. Each serial port is being processed in a...
22
by: Morpheus | last post by:
Hi, I have been coding in Windows for many years so have a mindset to it, so forgive any stupid questions. Is it possible to create a multithread application in C++ that is portable...
6
by: Sergey Poberezovskiy | last post by:
I have the following code in C# that I have trouble converting to VB(2.0): private delegate void openDialog(); private void openWindowsDialog(openDialog open) { Thread thread = new Thread(new...
34
by: Creativ | last post by:
Why does Thread class not support IDisposable? It's creating quite some problem. Namely, it can exhaust the resource and you have not control over it.
19
by: Hapa | last post by:
Does only reading (never writing) of a variable need thread synchronisation? Thanks for help? PS. Anybody knows a Visual C++ news group?
9
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I've got a routine that builds a table using different queries, different SQL Tables, and adding custom fields. It takes a while to run (20 - 45 seconds) so I wrote a thread to handle the table...
2
by: k3xji | last post by:
Hi all, This will probably be a long question/short answer, sorry, but I have wandered net about the subject and really feel cannot find just enough information.I want to ask my question by...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.