I think you still have a bug. At 32:48 you show the argument passing from one thread to another via std::thread's constructor. However, as you only pass by reference, there might be a situation where thread A (which calls std::jthread's constructor) runs to the end of the ctor and further before a single instruction in the new thread has been executed. In fact, the old thread could actually exit the scope of the referenced passed variables immediately after the constructor is done (let's say jthread obj is moved to another location via some mechanism so the thread lives on) and when the new thread starts to run, the references to the passed arguments are not valid anymore. There are only two solutions to this problem I can see: 1) You force the user to use a synchronisation primitive right at the start of his own thread to ensure that arguments had time to be copied/moved to the argument parameter list before unlocking the old thread (very unelegant and error prone). 2) You copy/move-construct all arguments into a closure and re-forward it at the invocation of the function. Another option would be to agree to some kind of contract which states that the passed arguments must stay in scope during the whole duration that task is running (and also not be tempered with). I think this is kind of hard to enforce.
Oh yeah been waiting for 'ol Nicolai!
Amazing hard work! Thanks a lot!
I think you still have a bug. At 32:48 you show the argument passing from one thread to another via std::thread's constructor. However, as you only pass by reference, there might be a situation where thread A (which calls std::jthread's constructor) runs to the end of the ctor and further before a single instruction in the new thread has been executed. In fact, the old thread could actually exit the scope of the referenced passed variables immediately after the constructor is done (let's say jthread obj is moved to another location via some mechanism so the thread lives on) and when the new thread starts to run, the references to the passed arguments are not valid anymore.
There are only two solutions to this problem I can see:
1) You force the user to use a synchronisation primitive right at the start of his own thread to ensure that arguments had time to be copied/moved to the argument parameter list before unlocking the old thread (very unelegant and error prone).
2) You copy/move-construct all arguments into a closure and re-forward it at the invocation of the function.
Another option would be to agree to some kind of contract which states that the passed arguments must stay in scope during the whole duration that task is running (and also not be tempered with). I think this is kind of hard to enforce.