473,467 Members | 2,491 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

multithreaded calling of static func

i have just a little question:

guess i have a class with a static function [lets say CA::static()] and
i have different threads running. do i have to expect the problem that
2 treads try to acces CA::static() at the same time or does the
processor always make shure that one of them has to wait till the other
finishes? or may it be that a static func can be executed twice at the
same time? or should i implement something like:

bool CA::fInUse = false;

CA::static() {
while (fInUse) ;
fInUse = true;
// do something //
fInUse = false;
}

Sep 28 '06 #1
3 1992
cl***@yahoo.de wrote:
i have just a little question:

guess i have a class with a static function [lets say CA::static()]
That wouldn't be a valid name.
and i have different threads running. do i have to expect the problem that
2 treads try to acces CA::static() at the same time or does the
processor always make shure that one of them has to wait till the other
finishes? or may it be that a static func can be executed twice at the
same time?
Standard C++ doesn't cover threads, so it depends on the implementation.
or should i implement something like:

bool CA::fInUse = false;

CA::static() {
while (fInUse) ;
fInUse = true;
// do something //
fInUse = false;
}
This is probably not enough.

Sep 28 '06 #2
cl***@yahoo.de wrote:
i have just a little question:

guess i have a class with a static function [lets say CA::static()]
and i have different threads running. do i have to expect the problem
that 2 treads try to acces CA::static() at the same time or does the
processor always make shure that one of them has to wait till the
other finishes? or may it be that a static func can be executed twice
at the same time? or should i implement something like:

bool CA::fInUse = false;

CA::static() {
while (fInUse) ;
fInUse = true;
// do something //
fInUse = false;
}
First of all, let me mention that threading is not part of C++ language
and because of that discussions on threads are generally off-topic here.
Try comp.programming.threads. But here is a couple of thoughts:

If you have to ask, it's time for you to study those things seriously.
Threading cannot be explained in a single newsgroup posting, please
refer to 'comp.programming.threads' and their recommendation of books
and start systematic learning of it.

Functions can be called from different threads, it's the *data* that
should concern you. Access to data often has to be paid the most
attention to. To simplify it: two threads should not attempt to write
to the same memory area "at the same time". Nor one should read while
the other one is writing. Doing those things causes trouble.

How to protect things from being screwed up in a multithreaded program,
you need to learn. It can be done from books. It can be done on the
'Net It can be "picked up" by reading documentation and looking at
somebody else's code, but that usually leads to forming serious
misconceptions which are difficult (though necessary) to un-learn later.
So, make THE RIGHT CHOICE(tm).

Good luck!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 28 '06 #3
cl***@yahoo.de wrote:
i have just a little question:

guess i have a class with a static function [lets say CA::static()] and
i have different threads running. do i have to expect the problem that
2 treads try to acces CA::static() at the same time
Yes.
or does the
processor always make shure that one of them has to wait till the other
finishes?
No.
or may it be that a static func can be executed twice at the
same time?
Yes. It may be. But that's no problem as long as you don't use static or
global variables. Functions that only use local variables are called
"thread-safe".

>or should i implement something like:

bool CA::fInUse = false;
better:
volatile bool CA::fInUse = false;
>
CA::static() {
while (fInUse) ;
fInUse = true;
// do something //
fInUse = false;
}
This might work, but of all solutions one can imagine, this
is the worst (the wait-loop would consume about 100% CPU-time).

If a multithreaded function accesses a non-local
object (e.g. a class-member or global/static objects),
you need to manage the access.
In a Windows-Enviroment this can be done by using Events
(CreateEvent, WaitForSingleObject).

Best regards, Martin


Sep 29 '06 #4

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

Similar topics

9
by: Simon Harvey | last post by:
Hi all, In my project I have made a number of helper methods static. As I understand it, this will create the problem that multiple threads could access the static method at the same time and...
1
by: Antal Rutz | last post by:
Hi! I wrote a little class to make multihreading easier. It's based on one of aahz's threading example scripts. What it does: It spawns up number of CollectorThreads and one ProcessThread. The...
3
by: Aryeh M. Friedman | last post by:
Why is the value of "Instance" whacked in the following code. Note this only happens in Graph::*Wrapper. Also please note that if I hardcode WalkFunc and PrintFunct to be BredthFirst and...
13
by: Alex Vinokur | last post by:
I have the following problem. class Base { public: void (*pfunc) (int) = 0; }; class Derived : public Base {
4
by: Gibby Koldenhof | last post by:
Hiya, I'm setting up some code in the spirit of Design Patterns, OOP, etc. All nice and well, it handles pretty much all OO style things and serves my purposes well. There's one last final...
6
by: Ulrika Ziverts | last post by:
Hello! I have a .NET application that communicates with an AS400 application through PCOM. I call an unmanaged function in the PCOM API to get a string back representing the screen in the host...
7
by: Kenneth Brody | last post by:
The recent thread on "query about main()" got me thinking... As I recall, calling a function with the wrong parameters causes undefined behavior. (These all assume that no prototype of foo()...
3
by: | last post by:
Is it possible to have just a multithreaded sub procedure? What I need is a timer time_elapsed event (2 sec interval) send params to a sub that is multithreaded. I have a COM component used to...
5
by: Joakim Hove | last post by:
Hello, I have a function like this: /* It actually does something else .... */ void func (int size, const char ** string_list) { int i; for (i=0; i < size; i++) printf("String number %d:...
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...
1
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...
0
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,...
0
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...
0
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 ...

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.