Forums

Unfortunately no one can be told what FluxBB is - you have to see it for yourself.

You are not logged in.

#1 2008-11-13 14:46:41

Franz
Lead developer
From: Germany
Registered: 2008-05-13
Posts: 4,070
Website

C++: stopping a while loop from outside

I have a class. And in that class, I have a loop like this:

while (true) {
  if (check) {
    break;
  }

  ....
}

Can I access the check variable from another class function during runtime?


fluxbb.de | develoPHP

"As code is more often read than written it's really important to write clean code."

Offline

#2 2008-11-13 15:36:10

Smartys
Former Developer
Registered: 2008-04-27
Posts: 3,135
Website

Re: C++: stopping a while loop from outside

If you define check outside of the while loop. Of course, in a single-threaded app, another class won't be able to set check once you're in the while loop because you'll be looping.
(I don't actually know C++, but there's no reason for me to believe this is wrong).

Offline

#3 2008-11-13 15:54:11

Reines
Lead developer
From: Scotland
Registered: 2008-05-11
Posts: 3,165
Website

Re: C++: stopping a while loop from outside

What Smartys said. Also is there any reason to use that instead of:

while (!check) {
    ...
}

Offline

#4 2008-11-14 15:14:47

Franz
Lead developer
From: Germany
Registered: 2008-05-13
Posts: 4,070
Website

Re: C++: stopping a while loop from outside

@reines... uhmmm no. you're right. any ideas how i could do it instead?

Last edited by Franz (2010-08-09 20:51:11)


fluxbb.de | develoPHP

"As code is more often read than written it's really important to write clean code."

Offline

#5 2008-11-14 15:30:31

jmp
Member
Registered: 2008-05-03
Posts: 85

Re: C++: stopping a while loop from outside

lie2815 wrote:

@reines... uhmmm know. you're right. any ideas how i could do it instead?

Didn't he just say it?

Offline

#6 2008-11-15 12:58:50

Franz
Lead developer
From: Germany
Registered: 2008-05-13
Posts: 4,070
Website

Re: C++: stopping a while loop from outside

Well, as Smartys said (and as Reines referenced Smartys), this won't work in a single-threaded app...

But I found a solution. Never mind.


fluxbb.de | develoPHP

"As code is more often read than written it's really important to write clean code."

Offline

#7 2008-11-15 13:21:51

Strofanto
Member
From: Italy
Registered: 2008-05-14
Posts: 288

Re: C++: stopping a while loop from outside

Shouldn't you use return instead of break?

Last edited by Strofanto (2008-11-15 13:24:51)

Offline

#8 2008-11-15 13:24:27

Franz
Lead developer
From: Germany
Registered: 2008-05-13
Posts: 4,070
Website

Re: C++: stopping a while loop from outside

I don't think so. Same effect, but also same problem... single-threaded app.

I'll just refrain from doing an infinite loop


fluxbb.de | develoPHP

"As code is more often read than written it's really important to write clean code."

Offline

#9 2008-11-15 13:31:53

dotme
Member
Registered: 2008-11-15
Posts: 2

Re: C++: stopping a while loop from outside

Even in a single-threaded application you might use signal handlers like SIGALRM.

Offline

#10 2008-11-15 13:34:38

Franz
Lead developer
From: Germany
Registered: 2008-05-13
Posts: 4,070
Website

Re: C++: stopping a while loop from outside

Could you explain a little more? I am not that experienced. Thank you.


fluxbb.de | develoPHP

"As code is more often read than written it's really important to write clean code."

Offline

#11 2008-11-15 13:48:10

dotme
Member
Registered: 2008-11-15
Posts: 2

Re: C++: stopping a while loop from outside

In Unix you can schedule an alarm (man alarm, man ualarm), which causes a signal of type SIGALRM send to you app. Before issuing the alarm, you have to setup a signal handler (sigaction ?) which acts on SIGALRM. You should ask google for some more details and examples.

Anyway your class has to make the variable "check" accessible from outside and you have to keep a reference to your class-instance on which you signal handler can act.

Offline

#12 2008-11-15 13:50:29

Franz
Lead developer
From: Germany
Registered: 2008-05-13
Posts: 4,070
Website

Re: C++: stopping a while loop from outside

Well, thank you. I will probably have a look at that later, but I'll just choose the simple way (see above) for this project, since I don't have enough time...

Thanks.


fluxbb.de | develoPHP

"As code is more often read than written it's really important to write clean code."

Offline

#13 2008-11-16 12:08:23

frozen_space
Member
From: Wuxi, China
Registered: 2008-05-12
Posts: 107
Website

Re: C++: stopping a while loop from outside

you can write a value to a file, and within that loop, check the value of the file


Today is the tomorrow you worried about yesterday, and all is well. smile
FluxBB in Chinese.

Offline

#14 2008-11-16 13:06:21

Franz
Lead developer
From: Germany
Registered: 2008-05-13
Posts: 4,070
Website

Re: C++: stopping a while loop from outside

I would still need to be able to write to that file while doing the loop... but thx


fluxbb.de | develoPHP

"As code is more often read than written it's really important to write clean code."

Offline

#15 2008-11-18 08:52:57

bjorn
Member
Registered: 2008-11-18
Posts: 1

Re: C++: stopping a while loop from outside

The only thing saving you in a single threaded application would be interrupts. And in most x86 compilers even interrupts are  handled in-thread, so you're mostly stuck.  Implenting threading in cpp is VERY easy though.

That being said, a problem needing a solution like the one you're explaining mostly represents bad logic and coding. mind telling us more about what you really want to do?

Offline

#16 2008-11-18 14:45:18

Franz
Lead developer
From: Germany
Registered: 2008-05-13
Posts: 4,070
Website

Re: C++: stopping a while loop from outside

It's actually done, but the topic is interesting...

I was working on a simulation of a street with two gas stations. Every second there is a car, 1 of 60 cars stops and so on...

I hope that's enough explanation.


fluxbb.de | develoPHP

"As code is more often read than written it's really important to write clean code."

Offline

#17 2008-11-19 12:56:35

Kaboon
Member
Registered: 2008-05-28
Posts: 24
Website

Re: C++: stopping a while loop from outside

lie2815 wrote:

It's actually done, but the topic is interesting...

I was working on a simulation of a street with two gas stations. Every second there is a car, 1 of 60 cars stops and so on...

I hope that's enough explanation.

Couldn't that just be easily be done by doing;

    if((CURRENT_CAR % 60) == 0)
        // do some kind of action here

in the loop or am I missing the point here? smile

Last edited by Kaboon (2008-11-19 12:56:51)

Offline

#18 2008-11-19 18:16:06

Franz
Lead developer
From: Germany
Registered: 2008-05-13
Posts: 4,070
Website

Re: C++: stopping a while loop from outside

It's a simulation. It's done randomly.

if (random(60) == 59)
   newCar();

fluxbb.de | develoPHP

"As code is more often read than written it's really important to write clean code."

Offline

Board footer

Powered by FluxBB 1.5.0