Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > multithreaded calling of static func

Reply
Thread Tools

multithreaded calling of static func

 
 
clqrq@yahoo.de
Guest
Posts: n/a
 
      09-28-2006
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;
}

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      09-28-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-28-2006
(E-Mail Removed) 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


 
Reply With Quote
 
Martin Steen
Guest
Posts: n/a
 
      09-28-2006
(E-Mail Removed) 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




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
func() in implementation and func (100, 200, 300) in calling Alex Vinokur C Programming 6 11-18-2006 08:08 AM
href="javascript:func()" vs href="#" onclick="javascript:func()" CRON HTML 24 06-20-2006 08:05 PM
difference between import from mod.func() and x=mod.func() Hari Sekhon Python 0 06-20-2006 08:07 AM
What's the difference between built-in func getattr() and normal call of a func of a class Johnny Python 3 08-23-2005 08:28 AM
int func() v/s int func(void) Vinko Vrsalovic C Programming 14 01-24-2005 08:07 AM



Advertisments