I am writing a custom user-level thread library using he C functions getcontext(), setcontext(), and makecontext(). I am having difficulty in the implementing logic of thread_fork() function. Basically, thread_fork() function creates new thread and when returns from thread_fork(), newly created thread should receive a return value of 0 while the original thread should receive the threadid as return value.
void Thread1Example(int) {
std::cout << "B" << std::endl;
thread_exit();
}
void main() {
Init(); // Initialize thread library data structures
std::cout << "A" << std::endl;
int thread1 = thread_fork(); // Fork a new thread and returns 0 & threadID
if (thread1==0)
Thread1Example();
std::cout << "C" << std::endl;
}
~~~~~~~~~~~~~~~
Console Output:
ABC
I want to know some details how a thread_fork() function can return two different values using C Context library (setcontext(), getContext, etc).
Aucun commentaire:
Enregistrer un commentaire