Code Monkey home page Code Monkey logo

jthread's People

Contributors

anthonywilliams avatar brandl-muc avatar hotwatermorning avatar josuttis avatar jwakely avatar kalman5 avatar lewissbaker avatar nathan-alden-sr avatar sparik avatar toughengineer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jthread's Issues

stop_token/stop_source method bikeshedding

LEWG suggested using the "request" and "requested" terminology to indicate that the operation is an asynchronous request to stop and that the operation is not stopped immediately.

Also, I think the name change from interrupt to stop is an improvement.

This then raises the question about what to name the methods.

Some possibilities to consider:

  • stop_source
    • signal_stop() => request_stop()
    • valid() => stop_requestable() or stoppable() or can_be_stopped() (or just leave as valid())
    • stop_signalled() => stop_requested()
  • stop_token
    • stop_signalled() => stop_requested()
    • stop_done_or_possible() => stop_requestable() or stoppable() or can_be_stopped() or can_stop_be_requested()

Non-reserved names

The stop_token header uses labels named load_state and check_state which need to be __uglified for use in std::lib implementations.

Algorithms with stop_token?

First off, jthreads are a very interesting proposal for the std lib, I hope you drive it home. Thanks for your work.

Having a means of collaboratively requesting a halt of a chunk of work doesn't end with jthreads. It would be immensely useful to be able to pass the stop_token to other parts of the library, particularily std algorithm. Currently the way to stop e.g. std::sort on a large array is to pass it a predicate that throws an exception when stop is requested. This approach has issues of its own: exception safety, the need to wrap all sort calls with such predicates in try blocks, not usable in codebases with disabled exceptions, etc. Not to mention treating a perfectly normal case in GUI programming (user clicked away and wants to see another item list) as exceptional is somewhat hackish.

Are there any plans to respect the stop_token in other parts of the standard library?

[Q] jthread with shared stop_source

Is there any particular reason why jthread does not allow constructing with external stop_source?
Was this ever considered in the standards?
Can't see any problems with such constructor, so just curious why it isn't in the library.
Well, the only ambiguity case I can find is whatever to do if the stop_source is already stopped.

int main()
{
  std::stop_source s;
  std::jthread p1{s, [](std::stop_token t){ // (does not work)
    doWork1(t);                             // t = s.get_token()
  }};

  std::jthread p2{s, [](std::stop_token t){  // (does not work)
    doWork2(t);                              // t = s.get_token()
  }};

  s.request_stop(); // stop both threads.
}

Obviously, this is trivial example, but more realistic scenarios can be a thread group, or dependent threads, that may benefit a shared stop_source.

Bug in condition_variable_any2

There's a bug in condition_variable_any2 that I don't think impacts the implementation of jthread. However this is such a complex subject that no stone should be left unturned.

~condition_variable_any();

Requires: There shall be no thread blocked on *this. [Note: That is, all threads shall have been notified; they may subsequently block on the lock specified in the wait. This relaxes the usual rules, which would have required all wait calls to happen before destruction. Only the notification to unblock the wait needs to happen before destruction. The user should take care to ensure that no threads wait on *this once the destructor has been started, especially when the waiting threads are calling the wait functions in a loop or using the overloads of wait, wait_for, or wait_until that take a predicate.
—end note]

That big long note means ~condition_variable_any() can execute before a signaled thread returns from a wait. If this happens with condition_variable_any2, that waiting thread will attempt to lock the destructed mutex mut. To fix this, there must be shared ownership of the data member mut between the condition_variable_any object and the member functions wait (wait_for, etc.).

libc++'s implementation gets this right: https://github.com/llvm-mirror/libcxx/blob/master/include/condition_variable

It holds the data member mutex with a shared_ptr<mutex> instead of mutex directly, and the wait functions create a local shared_ptr<mutex> copy on entry so that if *this destructs out from under the thread executing the wait function, the mutex stays alive until the wait function returns.

FYI – discussion of this proposal on the structured concurrency forum

There's been a lot of discussion happening over the last few years about "structured concurrency", across a number of different languages – we even started a forum recently, with participation from folks designing Python, Kotlin, Java, Swift, etc. I just heard about this proposal, and it's very closely related to the kinds of things we're talking about. So, I figured I'd drop you an invitation to join in, if you like! I also made a post summarizing this proposal for folks there, and comparing it to how other languages have approached this, which you might be interested in:

https://trio.discourse.group/t/structured-concurrency-proposal-for-c-20/125

Question about the new definition std :: jthread

How different is the std::thread from std::jthread?
Does it make sense to change definitions for c++20?
Is std::jthread only under c++20?
I was able to compile std::jthread under c++17. Is it compatible with mingw.thread.h?

[Q] Explicitly supplied stop_token in ctor results in (maybe) surprising result from get_stop_token()

int main()
{
	auto token = std::stop_token{};
	auto thrd = std::jthread{[](std::stop_token){}, token};
	assert(thrd.get_stop_token() == token); // surprise

	return EXIT_SUCCESS;
}

Just small question regarding the intended way the jthread ctor is used, because that was shown to me today as an alternative and valid way to use a jthread. I think that should not be allowed or otherwise the explicitly supplied stop_token should be copied/moved into the jthread so that the assertion holds. Or else the result of get_stop_token() may "surprise" when used IMHO.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.