strange behavior in simple multithread program

So my code pretty much works the way i want it to. There is just some strange behavior that I was hoping you guys could shed some light on.

It's supposed to count until the user passes it a 0 and then output how high it was able to count in that span of time. If the user passes it anything else other than a zero it should ignore that input. If I run it, wait a bit, then pass it a zero, it works perfectly. If I run it and pass it as many 1's as i like, it ignores all of those inputs until i pass it a 0 and then again, it works perfectly. But here's the rub, if i pass it anything > than 1 it stops responding. Anything greater than 1 should evaluate the exact same way as a 1. so i just don't understand. Here is code, thanks in advance to everyone who takes a look.

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>

bool input = 1;
long long int count = 0;

void loop() {
	while (input) {
		++count;
	}
}

int main()
{
	std::thread t1(loop);
	t1.detach();
	while (input) {
		std::cin >> input;
	}
	std::cout << count;
	int pause; 
	std::cin >> pause; //so i can see the output
	return 0;
}
Why exactly are you using a function when you just put the while statement in main.
because i don't know any other way to use a thread yet. if you could show me the analogue to this program that does that same thing with out using a function i would be very grateful. i just based this on some example code that I've seen and that code used functions.
Last edited on
Sorry I am unfamiliar with threads.

EDIT
Again sorry.
But you might have helped me with a problem I was having thanks. :)
Last edited on
Thanks for taking a look either way : )
Last edited on
nvm i get it. im dumb. this is why im still in the newbie section. input needs to be an int not a bool. derp.
Topic archived. No new replies allowed.