473,386 Members | 1,738 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.

class instances and threads

hi,

I'd like to know if i need mutexs to lock the use of
member methods of a class instance shared between
several threads via a pointer? if the method modify
member variables, i know that mutexs are needed but
the case that interrest me is when the method never
modify a member variable. I really have no idea cause
i don't know if the process duplicate the method in
memory for each threads or if the local variables of
this method are shared by all the threads.

thanks a lot for your help, i just begin C++ and i
don't figure out all the subtleties of classes yet

--
lucas
Montes
EPITECH
Jul 23 '05 #1
6 2384
ouech wrote:
hi,

I'd like to know if i need mutexs to lock the use of
member methods of a class instance shared between
several threads via a pointer? if the method modify
member variables, i know that mutexs are needed but
the case that interrest me is when the method never
modify a member variable. I really have no idea cause
i don't know if the process duplicate the method in
memory for each threads or if the local variables of
this method are shared by all the threads.

thanks a lot for your help, i just begin C++ and i
don't figure out all the subtleties of classes yet


C++ classes does not change threading issues related to concurrent access.

In other words, if you need to lock your critical regions without
classes, you'll need to do the same with classes and C++ specifically
(in the current revision of the standard) does not specify thread semantics.

Having said that, many C++ classes STL types are thread safe to some
level (not for concurrent access though).

If *all* your threads are simply not modifying class state, then most
implementations of C++ will not require any locking of critical regions.

Jul 23 '05 #2
"ouech" <lo******@nospamhotmail.com> wrote in message
news:opsldexeaaozxxof@loucs...
I'd like to know if i need mutexs to lock the use of
member methods of a class instance shared between
several threads via a pointer? if the method modify
member variables, i know that mutexs are needed but
the case that interrest me is when the method never
modify a member variable. I really have no idea cause
i don't know if the process duplicate the method in
memory for each threads or if the local variables of
this method are shared by all the threads.


This really has nothing to do with C++ (the current C++
standard specifies nothing about multi-threaded platforms).

But on common platforms, as long as all accesses to an
object/memory location are read-only, no synchronization
is required. As soon as one of the threads may modify
the object, a synchronization is required for all threads
(including the read-only ones).

On most C++ platforms, the previous can also be applied
to standard library containers: read-only access (i.e.
usage of 'const' member functions only) demands no
explicit synchronization.
I hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 23 '05 #3
ouech wrote:
hi,

I'd like to know if i need mutexs to lock the use of
member methods of a class instance shared between
several threads via a pointer? if the method modify
member variables, i know that mutexs are needed but
the case that interrest me is when the method never
modify a member variable. I really have no idea cause
i don't know if the process duplicate the method in
memory for each threads or if the local variables of
this method are shared by all the threads.

thanks a lot for your help, i just begin C++ and i
don't figure out all the subtleties of classes yet

--
lucas
Montes
EPITECH


A mutex is only needed if the multiple threads will be making changes to
shared data. Local variables of a method are not shared data, they are
unique to each thread.

--
Scott McPhillips [VC++ MVP]

Jul 23 '05 #4
Scott McPhillips [MVP] wrote:
ouech wrote:
hi,

I'd like to know if i need mutexs to lock the use of
member methods of a class instance shared between
several threads via a pointer? if the method modify
member variables, i know that mutexs are needed but
the case that interrest me is when the method never
modify a member variable. I really have no idea cause
i don't know if the process duplicate the method in
memory for each threads or if the local variables of
this method are shared by all the threads.

thanks a lot for your help, i just begin C++ and i
don't figure out all the subtleties of classes yet

--
lucas
Montes
EPITECH

A mutex is only needed if the multiple threads will be making changes to
shared data. Local variables of a method are not shared data, they are
unique to each thread.


Is this stated in the standard?

Is this true for all platforms?
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 23 '05 #5

Thomas Matthews wrote:
Scott McPhillips [MVP] wrote:
A mutex is only needed if the multiple threads will be making changes to shared data. Local variables of a method are not shared data, they are unique to each thread.


Is this stated in the standard?

Is this true for all platforms?


Each thread has its own execution stack. Local non-static variables in
one thread are independent of the "same" local non-static variables in
another thread. This is true for all multi-threaded platforms I know
of.

Hope this helps,
-shez-

Jul 23 '05 #6

"Shezan Baig" <sh************@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...

Thomas Matthews wrote:
Scott McPhillips [MVP] wrote:
A mutex is only needed if the multiple threads will be making changes to shared data. Local variables of a method are not shared data, they are unique to each thread.


Is this stated in the standard?

Is this true for all platforms?


Each thread has its own execution stack. Local non-static variables in
one thread are independent of the "same" local non-static variables in
another thread. This is true for all multi-threaded platforms I know
of.


You missed Thomas' point. Standard C++ (the topic
of this newsgroup) has no direct support for threads at all.
Such support must be provided by extensions or third party tools.

-Mike
Jul 23 '05 #7

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

Similar topics

2
by: Rudi | last post by:
Is there anyone who can point me to a good tutorial on how to create a multithreaded class library? Class A is a controller that starts up a number of threads of code in Class B. Class B raises...
4
by: Roland Riess | last post by:
Hi all, at the moment i am developing an app which is sort of an interface to copy data from one database to another and it shall run as a service. As there are several databases the app must be...
3
by: Amy L. | last post by:
I have a method below called "ResolveHostname" which is called from the ThreadPool.QueueUserWorkItem. My concern is with the static dnsClient class where I am calling dnsClient.Lookup( Hostname)...
2
by: Tumurbaatar S. | last post by:
ASP.NET QuickStart Tutorial says that: .... ASP.NET maintains a pool of HttpApplication instances over the course of a Web application's lifetime. ASP.NET automatically assigns one of these...
10
by: Fred Mertz | last post by:
I'm wondering what some of the important considerations are regarding implementing the DAL as a static class vs creating instances of it as needed. I'm writing a .NET 2.0 Windows application...
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.
10
by: Terry | last post by:
Hi, Is there a simple way to get all the instances of one class? I mean without any additional change to the class. br, Terry
5
by: pgrazaitis | last post by:
I cant seem to get my head wrapped around this issue, I have myself so twisted now there maybe no issue! Ok so I designed a class X that has a few members, and for arguments sake one of the...
0
by: IanWright | last post by:
Right, here goes. I'm designing a class library that runs a heuristic on several threads and therefore has to be thread safe. I'm going to outline the current design in a second, but I'm not sure...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.