many infinit loop

I want to have three infinit loop in my program (c++ lang) . is it possible or impossible ???
I use this code for creat infinit loop and I want use three infinit loop ; is it possible ?
1
2
3
4
5
6
7
while(3!=2)
{	
Sleep(2000);
system("color 4E");
Sleep(2000);
system("color 4f");
}


this code is one infinit loop . is it possible three this loop run one time ? How write this code?

Last edited on
The common construct for creating an infinite loop is

1
2
3
4
for (;;) 
{

}

Last edited on
thank you Duthomhas
I want use three infinite loop in my program and this loops run same time . is it possible three infinite loop run same time ?
is it possible three infinite loop run same time ?

What exactly do you mean by 'same time'? Every loop on its own CPU? Independent? Different pace? If for the last two questions the answer is No -- what connotes in sync -- you should consider a single loop incorporating the three tasks.
What are your reasons for three loops?

BTW, while(3!=2) {...} works full well as infinite loop.
Run each loop in a separate thread. For instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <thread>
#include <chrono>

void infinite_loop( char ch, int pause_milli_secs )
{
   while(true)
   {
       std::cout << ch << std::flush ;
       std::this_thread::sleep_for( std::chrono::milliseconds(pause_milli_secs) ) ;
   }
}

int main()
{
    std::cout << "press enter to quit program\n" << std::flush ;

    // start three infinite loops
    std::thread( infinite_loop, '-', 100 ).detach() ;
    std::thread( infinite_loop, '*', 300 ).detach() ;
    std::thread( infinite_loop, '!', 1000 ).detach() ;

    std::cin.get() ;
}
if you can combine the loops into one, it may be a better approach. Why do you need 3 of them? Are they just listeners or timers?
Last edited on
It looks to me like you have a design flaw.

What is it you are trying to accomplish with these three loops?
First thank you (Duthomhas,MikeStgt,JLBorges and jonnin)
Second : I want this loops run in same times . Means of "same time":

1. for example first loop is :
1
2
3
4
5
	while(1!=2)
	{
    	cout<<"Fist infinite loop is run\n"<<endl;
         Sleep(2000);
        }

Output :
Fist infinite loop is run

2. for example second loop is :
1
2
3
4
5
	while(1!=2)
	{
    	cout<<"second infinite loop is run\n"<<endl;
       Sleep(3000);
       }


Output :
second infinite loop is run

3. for example third loop is :
1
2
3
4
5
	while(1!=2)
	{
    	cout<<"third infinite loop is run\n"<<endl;
        Sleep(4000);
        }


Output :
third infinite loop is run

4. When I use three loop in my program :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	while(1!=2)
	{
    	cout<<"Fist infinite loop is run\n"<<endl;
        Sleep(2000);
        }
        while(1!=2)
	{
    	cout<<"second infinite loop is run\n"<<endl;
        Sleep(3000);
       }
	while(1!=2)
	{
    	cout<<"third infinite loop is run\n"<<endl;
        Sleep(4000);
        }


Output is show only "first infinite loop is run"
is it possible third infinite loop code is run while not stop any loop ?
console output :
1
2
3
4
5
6
7
Fist infinite loop is run
second infinite loop is run
third infinite loop is run
Fist infinite loop is run
second infinite loop is run
third infinite loop is run
....


three loop code is run at differente sleep time . loop first run 20 second second loop run 30 second and third loop run 40 second .
Last edited on


jonnin
if you can combine the loops into one, it may be a better approach. Why do you need 3 of them? Are they just listeners or timers?

A timer can be named . for previous post , how use this code with timer insted of infinite loop ?
You could factor out the LCM of your intervals, and then use a simple state machine to decide what to do.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <chrono>
#include <thread>

int main ( ) {
    unsigned int count = 0;
    for ( ; ; count++ ) {
        if ( count % 2 == 0 ) {
            std::cout<<"First infinite loop is run"<<std::endl;
        }
        if ( count % 3 == 0 ) {
            std::cout<<"second infinite loop is run"<<std::endl;
        }
        if ( count % 4 == 0 ) {
            std::cout<<"third infinite loop is run"<<std::endl;
        }
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
}


$ g++ -std=c++11 foo.cpp
$ ./a.out 
First infinite loop is run
second infinite loop is run
third infinite loop is run
First infinite loop is run
second infinite loop is run
First infinite loop is run
third infinite loop is run
First infinite loop is run
second infinite loop is run
First infinite loop is run
third infinite loop is run
second infinite loop is run
First infinite loop is run
First infinite loop is run
//// and so on 

Means of "same time": ...Sleep(2000)

I assume Sleep() takes milliseconds as argument, 2000 milliseconds = 2 seconds (not 20 as you state). Thus your "same time" is in the range of seconds. Most of the time your loops do nothing but sleep, so there is plenty of time to do some housekeeping as salem c suggested -- http://www.cplusplus.com/forum/general/253431/#msg1114669

In case the sleep() is only a placeholder for real work within your loops you may consider using a thread for each loop in the hope your OS will distribute the workload on all cores of your processor(s). JLBorges showed a nice example -- http://www.cplusplus.com/forum/general/253431/#msg1114482

In case you try to "emulate" something like the 41C PIL Interface Chip (1LB6) connecting two systems with differently clocks, it could be advantageous to use threads even though there is enough time to simulate all on a single core.
Solve it . Thank you for all .
Have good time
Topic archived. No new replies allowed.