Threads
Nov 26, 2012 at 3:35pm UTC
I'm having some problem with threads. I'm using the visual studio 2012 ultimate.
Here.'s my code:
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
void func1()
{
for (int i = 0; i < 100; i++)
printf("hi\n" );
}
void func2()
{
for (int i = 0; i < 100; i++)
printf("bla\n" );
}
int main(int argc, char **argv)
{
std::thread thread1 = std::thread(func1);
std::thread thread2 = std::thread(func2);
thread1.join();
thread2.join();
std::cin.get();
return 0;
}
But the output I get is quite wrong: first i get 100x hi and then 100x bla.
Isn't threading supposed to do random outputs in this situation?
Nov 26, 2012 at 3:41pm UTC
Isn't threading supposed to do random outputs in this situation?
No, in this case
func1()
consumes all the cpu time and doesn't let the second thread run untill it's finished. Don't rely on any specific behavior regarding thread unless you introduced some synchronizing mechanisms
Nov 26, 2012 at 3:42pm UTC
Isn't threading supposed to do random outputs in this situation?
What threading is supposed to do is execute the functions in different threads. You shouldn't worry about or expect a particular order of output.
Topic archived. No new replies allowed.