void Mythread::threadSchedule(int n)
{
if(threadMap_.size() > 1)
{
int lastId = current_;
if(current_ != (--threadMap_.end())->first)
{
auto it = threadMap_.find(current_);
current_ = (++it)->first;
}
else
{
current_ = threadMap_.begin()->first;
}
swapcontext(threadMap_[lastId].get(),threadMap_[current_].get());
}
}
I recently saw a user-level thread implemented with ucontext, I just don't understand how the threads switch. In the code above, after this swapcontext
done its job, the context of threadMap_[lastId].get()
rather should be inside the threadSchedule
? But the context is in the thread with lastID break point. It's just so confused that "current context" which swapcontext(ucontext_t *oucp, ucontext_t *ucp)
saved to the oucp
is not the context inside the function threadSchedule(int n)
now but the old thread's context. The whole code is in https://github.com/Miaoshuai/Coroutine