-1

Hello i am trying to run two functions at the same time. Because i want to learn to make a timer or countdown of some kind.

And i have an idea on how to do so. But when i create two threads.

I get no output in my console application.

Here is my code.

#include <iostream>
#include <Windows.h>
#include <thread>
#include <random>
#include <string>

void timer()
{
    int x{ 0 };
    if (x < 1000)
    {
        std::cout << x++;
        Sleep(1000);
    }

}
void timer2()
{
    int x{ 0 };
    if (x < 10000)
    {
        std::cout << x++;
        Sleep(1000);
    }
     
}
int main()
{
    std::thread one(timer);
    std::thread two(timer2);
     
    one.detach();
    two.detach();
    return 0;
} 
  • Why `one.detach(); two.detach();` instead of `one.join(); two.join();`? – jabaa Aug 20 '22 at 18:52
  • I get a program crash if i do one.join on both of them. – peter evans Aug 20 '22 at 18:53
  • But with `detach` the main thread won't wait for the other two threads. It will stop them almost immediately. How does your program crash with `join`? – jabaa Aug 20 '22 at 18:54
  • So when do i use detach or do i not use it at all in this case? – peter evans Aug 20 '22 at 18:55
  • Not using it all causes undefined behavior or an error. You can't destroy a thread without `join` or `detach`. `detach` is the reason that the main thread won't wait for the two threads and you don't see the output because your program exits almost immediately. – jabaa Aug 20 '22 at 18:56
  • Using one.join(); and two.join(); works but only prints out 1,1 and ends. How do i use the detach if it will close, Undernath the thread.join? Like how do i prevent it from immeditality closing. – peter evans Aug 20 '22 at 18:57
  • Why do you insist in using `detach`? You want the opposite of `detach`. You want `join`. – jabaa Aug 20 '22 at 18:58
  • But you said if i don't detach i will get undefined behaviour? – peter evans Aug 20 '22 at 19:00
  • You have to `detach` or `join`. – jabaa Aug 20 '22 at 19:01
  • will it auto detach at the end? im a little confused i understand i have to join now. But why the detach? – peter evans Aug 20 '22 at 19:02
  • Do you know how i can seperate the lines so they don't end up doing 11223344 in the two threads? – peter evans Aug 20 '22 at 19:03
  • Please ask one question per question. Don't ask follow-up questions in the comments or modify the question to add new questions. – jabaa Aug 20 '22 at 19:07

2 Answers2

2

You should use join instead of detach. Otherwise, the main thread won't wait for the other threads and the program will exit almost immediately. You can use std::this_thread::sleep_for instead of Sleep to make the code portable (no Windows.h required).

#include <iostream>
#include <thread>
using namespace std::chrono_literals;

void timer()
{
    int x{ 0 };
    if (x < 1000)
    {
        std::cout << x++;
        std::this_thread::sleep_for(1s);
    }

}
void timer2()
{
    int x{ 0 };
    if (x < 10000)
    {
        std::cout << x++;
        std::this_thread::sleep_for(1s);
    }
     
}
int main()
{
    std::thread one(timer);
    std::thread two(timer2);
     
    one.join();
    two.join();
    return 0;
} 
jabaa
  • 5,844
  • 3
  • 9
  • 30
  • Thank you few people helped solve my problem here. Also how do i make it so both lines don't go 11223344556677, instead i want them executing 1 1 2 2 on seperating lines in a continued line – peter evans Aug 20 '22 at 19:16
  • @peterevans Please don't ask follow-up questions in comments. Ask a new question. – jabaa Aug 20 '22 at 19:16
0

You need to flush the output. C++ won't output anything until it flushes. You can achieve this by

cout << x++ << "\n";

or

cout << x++ << std::flush; 

according to this answer

Sahin
  • 1,032
  • 14
  • 23
  • Still doesn't output anything :( – peter evans Aug 20 '22 at 18:40
  • 1
    It probably returns(exits) before entering the functions you call. you can add this line to main function after calling detach `std::this_thread::sleep_for(std::chrono::seconds(5));` you may look this page https://en.cppreference.com/w/cpp/thread/thread/detach – Sahin Aug 20 '22 at 18:43
  • All this does is output 0, 0 and then it ends. – peter evans Aug 20 '22 at 18:44
  • 1
    Because you are calling x++. This will run the code with previous value of x and it will increment. Instead write ++x. Here is what i mean: https://en.cppreference.com/w/cpp/language/operator_incdec – Sahin Aug 20 '22 at 18:46
  • No that's not what i mean. I want it to do that. That still gives me the 1,1 output and not conintually increment from 1, 1000 on one thread and 1, 10000 on the other – peter evans Aug 20 '22 at 18:48
  • 1
    Did you mean to put `while(x < 1000)` instead of `if(x < 1000)`? There's nothing in a `std::thread` that will cause it to loop if you don't write a loop yourself. – Nathan Pierson Aug 20 '22 at 18:57
  • No i want it to increment from 0 to 1000 – peter evans Aug 20 '22 at 18:58
  • Sorry yes you are right. I was getting confused for a moment because ive been stuck for a while trying to understand whats wrong – peter evans Aug 20 '22 at 18:59
  • Do you know how i can seperate the lines so they don't end up doing 11223344 in the two threads? – peter evans Aug 20 '22 at 19:04
  • add << "\n" or std::endl after cout. You need a new line – Sahin Aug 23 '22 at 13:22