Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

it's not thread safe #10

Open
Igor-Mochalin opened this issue Mar 6, 2022 · 2 comments
Open

it's not thread safe #10

Igor-Mochalin opened this issue Mar 6, 2022 · 2 comments

Comments

@Igor-Mochalin
Copy link

Igor-Mochalin commented Mar 6, 2022

to reproduce

 for(int i=1;i<100;i++)
  std::thread(test1).detach();
   void test1()
   {
      Timer t = Timer();
      t.setInterval([&](){
         },
         1000);   
      t.stop();
   }

results
Exception has occurred.
Segmentation fault

@ckt1010
Copy link

ckt1010 commented Mar 14, 2022

yes, I think active variable is useless

@lilucpp
Copy link

lilucpp commented Jan 19, 2023

Because the Timer is destroyed after t.stop(), and the thread inside the setInterval function uses the Timer after the destruction.

An idea: Timer inherits std::enable_shared_from_this to extend the life cycle of Timer until the thread inside setInterval function ends.

Modified code:
timerrcpp.h

test program:
test.cpp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants