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

Skip waits if current thread is interrupted #73

Open
LnnrtS opened this issue Mar 11, 2021 · 3 comments
Open

Skip waits if current thread is interrupted #73

LnnrtS opened this issue Mar 11, 2021 · 3 comments
Assignees

Comments

@LnnrtS
Copy link
Contributor

LnnrtS commented Mar 11, 2021

When a thread is interrupted during some wait() (for example event_flags::wait()) it returns early with EINTR.

If interrupt was already set when entering the wait() it does not return early but still returns EINTR.
I think this is inconsistent.

As the interruption has to be manually reset (and does not reset itself after it once triggered an early return), I think the expected behavior is that all wait()s return early as long as the flag is set.

To achieve this, we would have to check the interrupted flag before any wait() suspends the current thread and just return in case it is set.

@ilg-ul
Copy link
Contributor

ilg-ul commented Mar 11, 2021

I don't remember the details why the status reflecting the interruption requires a manual reset, but at first sight this was intended as a feature, not a bug.

As for the behaviour when entering wait() while the interrupted flag is still set, this looks like an oversight.

Do you suggest that, as long as the flag remains set, all synchronisation waits should return immediately and return EINTR?

@ilg-ul ilg-ul self-assigned this Mar 11, 2021
@LnnrtS
Copy link
Contributor Author

LnnrtS commented Mar 11, 2021

Do you suggest that, as long as the flag remains set, all synchronisation waits should return immediately and return EINTR?

Yes, I think that's very useful, at least for my usecase.

thread workerThread
{
  [](void* arg) -> void*
  {
    while (true)
    {
       longRunningJobWithLotsOfWaits();
       if (this_thread::thread().interrupted()) break;
    }
  },
  nullptr
};

sysclock.sleep_for(1000);

// now I want to stop the thread immediately
workerThread.interrupt();
workerThread.join();

This would not be possible in a generic way if the interruption flag would reset itself.
Maybe this is actually what cancel() should do but it's not implemented yet.

@ilg-ul
Copy link
Contributor

ilg-ul commented Mar 11, 2021

Ok, we'll take a second look at POSIX thread interruption/cancellation and improve the behaviour.

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

No branches or pull requests

2 participants