Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Call to std::thread::join() in the destructor of a global variable

Call to std::thread::join() in the destructor of a global variable

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dchabaud
    wrote on last edited by
    #1

    I have the following code and when running it stays blocked on the join(). So, is it possible to call std::thread::join() in the destructor of a global variable?

    void MyTimerFunction();

    class MyGlobal
    {
    public:
    std::atomic<bool> m_Flag;
    std::thread m_GlobalThread;

    MyGlobal() {
        m\_Flag = true;
        m\_GlobalThread = std::thread(MyTimerFunction);
    }
    
    ~MyGlobal()
    {
        m\_Flag = false;
        if (m\_GlobalThread.joinable())
            m\_GlobalThread.join();
        std::cout << "MyGlobal destroyed" << std::endl;
    }
    

    };

    MyGlobal Test;

    void MyTimerFunction()
    {
    do
    {
    std::this_thread::sleep_for(std::chrono::seconds{10});
    if (!Test.m_Flag)
    break;

        std::cout << "New tick" << std::endl;
    } while (true);
    
    std::cout << "Exit MyTimerFunction" << std::endl;
    

    }

    int _tmain(int argc, _TCHAR* argv[])
    {
    std::cout << "Main thread sleeping..." << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds{15});
    std::cout << "Main thread awaking..." << std::endl;

    std::cout << "Main thread returning..." << std::endl;
    return 0;
    

    }

    CPalliniC 1 Reply Last reply
    0
    • D dchabaud

      I have the following code and when running it stays blocked on the join(). So, is it possible to call std::thread::join() in the destructor of a global variable?

      void MyTimerFunction();

      class MyGlobal
      {
      public:
      std::atomic<bool> m_Flag;
      std::thread m_GlobalThread;

      MyGlobal() {
          m\_Flag = true;
          m\_GlobalThread = std::thread(MyTimerFunction);
      }
      
      ~MyGlobal()
      {
          m\_Flag = false;
          if (m\_GlobalThread.joinable())
              m\_GlobalThread.join();
          std::cout << "MyGlobal destroyed" << std::endl;
      }
      

      };

      MyGlobal Test;

      void MyTimerFunction()
      {
      do
      {
      std::this_thread::sleep_for(std::chrono::seconds{10});
      if (!Test.m_Flag)
      break;

          std::cout << "New tick" << std::endl;
      } while (true);
      
      std::cout << "Exit MyTimerFunction" << std::endl;
      

      }

      int _tmain(int argc, _TCHAR* argv[])
      {
      std::cout << "Main thread sleeping..." << std::endl;
      std::this_thread::sleep_for(std::chrono::seconds{15});
      std::cout << "Main thread awaking..." << std::endl;

      std::cout << "Main thread returning..." << std::endl;
      return 0;
      

      }

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Quote:

      it stays blocked on the join()

      That's the correct behaviour: a never-ending thread won't join.

      In testa che avete, signor di Ceprano?

      D 1 Reply Last reply
      0
      • CPalliniC CPallini

        Quote:

        it stays blocked on the join()

        That's the correct behaviour: a never-ending thread won't join.

        D Offline
        D Offline
        dchabaud
        wrote on last edited by
        #3

        Not exactly, the destructor of the global variable sets an atomic to tell the thread to exit.

        CPalliniC 1 Reply Last reply
        0
        • D dchabaud

          Not exactly, the destructor of the global variable sets an atomic to tell the thread to exit.

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          Yes, you are right, my bad, I've overlooked it. Have you already read this one: "function static variable destructor and thread"[^]?

          In testa che avete, signor di Ceprano?

          D 1 Reply Last reply
          0
          • CPalliniC CPallini

            Yes, you are right, my bad, I've overlooked it. Have you already read this one: "function static variable destructor and thread"[^]?

            D Offline
            D Offline
            dchabaud
            wrote on last edited by
            #5

            Yeh, it seems to be the same thing. I checked under Linux with GCC and it works. On my point of view, Visual C++ runtime after exiting the main() destroys all the threads then all the global variables, instead of the opposite.

            CPalliniC 1 Reply Last reply
            0
            • D dchabaud

              Yeh, it seems to be the same thing. I checked under Linux with GCC and it works. On my point of view, Visual C++ runtime after exiting the main() destroys all the threads then all the global variables, instead of the opposite.

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              Looks a sensible hypothesis to me.

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups