You are not logged in.
- Topics: Active | Unanswered
Pages: 1
#1 2008-11-13 14:46:41
- Franz
- Lead developer

- From: Germany
- Registered: 2008-05-13
- Posts: 3,755
- 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?
Offline
#2 2008-11-13 15:36:10
- Smartys
- Former Developer
- Registered: 2008-04-27
- Posts: 3,117
- 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,140
- 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: 3,755
- Website
Offline
#5 2008-11-14 15:30:31
- jmp
- Member
- Registered: 2008-05-03
- Posts: 85
Re: C++: stopping a while loop from outside
@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: 3,755
- Website
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: 3,755
- Website
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
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: 3,755
- 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.
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. ![]()
FluxBB in Chinese.
Offline
#14 2008-11-16 13:06:21
- Franz
- Lead developer

- From: Germany
- Registered: 2008-05-13
- Posts: 3,755
- Website
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: 3,755
- 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.
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
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 herein the loop or am I missing the point here? ![]()
Last edited by Kaboon (2008-11-19 12:56:51)
Offline
#18 2008-11-19 18:16:06
Offline
Pages: 1
