C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

about member variables that are static, and threads

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
V-man
Guest





PostPosted: Fri Mar 05, 2004 11:59 pm    Post subject: about member variables that are static, and threads Reply with quote



I have a slight problem.

I have a class with it's own member variables and a member function.
I'm dealing with threads, so I made the variables and that mem
function static

class X
{
public:
static int value;
static void ThreadFuncToBeCalled();
};

void X::ThreadFuncToBeCalled()
{
.... running the thread .........
}
//there is more code of course, but this is the jist of it



the trouble is, since value has to be static, it is *shared* by all
the instances of X.
I want each instance of X to have it's own copy of value and at the
same time be static.

Or maybe there is a better solution you can recommend.

TIA

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Dhruv Matani
Guest





PostPosted: Sat Mar 06, 2004 10:09 am    Post subject: Re: about member variables that are static, and threads Reply with quote



On Fri, 05 Mar 2004 18:59:27 -0500, V-man wrote:

Quote:
I have a slight problem.

I have a class with it's own member variables and a member function.
I'm dealing with threads, so I made the variables and that mem
function static

class X
{
public:
static int value;
static void ThreadFuncToBeCalled();
};

void X::ThreadFuncToBeCalled()
{
.... running the thread .........
}
//there is more code of course, but this is the jist of it



the trouble is, since value has to be static, it is *shared* by all
the instances of X.
I want each instance of X to have it's own copy of value and at the
same time be static.

Or maybe there is a better solution you can recommend.

I actually did encounter this problem some time ago.
From my previous experience and the extra knowlegde that I have gained
because of a similar post on this group:

1. Ususlly a thread function returns a void* and accepts a void*.
2. Using this information, we can change all statics to non-statics.
3. Make the thread Proc accept a void* that is actually an X*.
4. Cast the void* to an X* inside the thread function, and use the members
like normal.
5. Would there would be 2 ways to get the above behaviour? I'm slightly
confused on this count:
a. One way seems natural which would be to make the data public, and let
anyone acces it.
b The 2nd method is slightly counter intuitive. I was thinking that we
could make the static member a friend of the class so that it may access
it's private parts, but since it is already a member, it already does have
those priviledges except that it may not access non-statics? So what is
the verdict of the gurus? Can the static member access the private
non-static members given a pointer to the class? Also I realized that
making a member function a friend of the class would again be
counter-intuitive.



--
Regards,
-Dhruv.

Proud to be a Vegetarian.
http://www.vegetarianstarterkit.com/
http://www.vegkids.com/vegkids/index.html


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Sat Mar 06, 2004 3:14 pm    Post subject: Re: about member variables that are static, and threads Reply with quote



In message <e1da64ba.0403050852.67931584 (AT) posting (DOT) google.com>, V-man
<j_carbone (AT) excite (DOT) com> writes
Quote:
I have a slight problem.

I have a class with it's own member variables and a member function.
I'm dealing with threads, so I made the variables and that mem
function static

I do not understand the logic of that statement.
Quote:

class X
{
public:
static int value;
static void ThreadFuncToBeCalled();
};

void X::ThreadFuncToBeCalled()
{
.... running the thread .........
}
//there is more code of course, but this is the jist of it



the trouble is, since value has to be static, it is *shared* by all
the instances of X.
I want each instance of X to have it's own copy of value and at the
same time be static.

Please explain what you mean by that statement. Are you using the word
'static' in the sense of being done at compile time, or is it that you
need a free function rather than a member function. Let me assume the
latter, then:

class X {
int value;
friend void ThreadFuncToBeCalled(X&)
public:
// ctors etc.
};

void ThreadFuncToBeCalled(X& x_ref){
// whatever
}

Note that there is no way of have distinct values of X::value without
either implicitly (as a member function) or explicitly via a declared
parameter telling the function which version it is to use.

Actually, strictly speaking that is not true because I could use a
global variable to identify which one to use such as:

X * active_X(0);

but all this really requires much more knowledge of your design
objectives than you have so far provided.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.