thread::join() worked 10 hours and continues to work. But when I use detach() , it takes from 50 minutes to 1 hour and 10 minutes, and the flow stops working.

 std::thread thr1(waitSignal); thr1.detach(); 

What am I doing wrong?

UPD

 void waitSignal(){ while(1){ std::ofstream log("/tmp/debug.txt", std::ios_base::app | std::ios_base::out); log << time << " - thread\n"; sleep(10); } } int iniReceive(){ // some code to start receive std::thread thr1(waitSignal); thr1.detach(); return(1); } int main() { iniReceive(); while(1){ printf("main"); std::ofstream log("/tmp/debug.txt", std::ios_base::app | std::ios_base::out); log << time << " - MAIN\n"; sleep(60); } return(0); } 
  • one
    What does your main thread do? And as you know, he stops working. Please provide the entire code. - Unick
  • It stops working because it stops writing time to the file. While the main thread continues to record the current time in the file. - Nazar Vozniy
  • What compiler is used? For a start, I would recommend removing the log object from the loop so as not to perform creation / destruction on each iteration and use different files. Maybe there is a problem with simultaneous access to the file from different streams. - αλεχολυτ
  • I use Eclipse. Began to use the file to specifically track the problem. Prior to this, instead of writing to a file, the LED blinked when a parallel stream was running, but it also stopped flashing after an hour. - Nazar Vozniy
  • Eclipse is a development environment, not a compiler ... - Harry

1 answer 1

I don't like two things terribly - what
1. uses the same file, while
2. It is idle in the open state all the time. You would at least close it before calling sleep , or something ... or even better, you have “cracked” a mutex ...

And then, with join() your main thread does not climb to the file, waits for completion ... and with detach() they work in parallel - collisions begin. What you observe may be a simple fast - because the main thread also keeps the file constantly open, closing only for the duration of the printf("main"); output printf("main"); .

  • Began to use the file to specifically track the problem. Prior to this, instead of writing to a file, the LED blinked when a parallel stream was running, but it also stopped flashing after an hour. - Nazar Vozniy
  • Try to run the following code like this - vpaste.net/15tqZ - and then tell me how the results ... - Harry
  • To understand why the LED does not blink when there is no information, how and why it blinks - this is a completely different task. I am responding to the task you posed in your question. And the LED - how can I know ("how can you understand something, if you don’t say anything?" ( ) Ivan Vasilyevich, who changed his profession ... :)) - Harry
  • one
    "Until then, instead of writing to the file, the LED blinked when the parallel stream was working" and on which device do you start, if you can blink the LED? - Unick
  • 17:07:50 - main 17:08:20 - main 17:08:25 - main 17:08:30 - main 17:08:35 - main 17:08:40 - main 17:09:25 - main 17 : 10:00 - main 17:10:05 - main 17:10:10 - main - Nazar Vozniy