- Can I setjmp in one thread and longjmp to the saved jmp_buf in
another?
If you wonder why I would like to do this evilness, the reason is that
Windows has no fork system call. So I wondered if fork could be faked
with non-local goto and threads, obviously with the limitation that no
new address space is created. To my knowledge, setjmp is the only
function apart from fork that returns twice.
Regards,
Sturla
Typically fork() is used as part of executing a new process. In a
Linux environment if you wanted threads in the same process you'd use
threads (which are a new type of process but that's not the point).
But why not just re-write your program so it does this
if (fork()) {
patha();
} else {
pathb();
}
return 0; /* end of function */
}
That way you can port it to threads in Windows by just calling pathb()
in a thread.
Tom
This question is platform dependent and cannot be answered within the
context of the C standard.
Anyway, threads usually comprise three things:
- a set of register values
- a stack
- some kind of thread identity within the operating system
The context saved by setjmp typically includes only a set of register
values. Thus calling jmp_buf will use
- the stack of the thread that called setjmp
- the identity of the thread that called setjmp
> If you wonder why I would like to do this evilness, the reason is that
> Windows has no fork system call. So I wondered if fork could be faked
> with non-local goto and threads, obviously with the limitation that no
> new address space is created. To my knowledge, setjmp is the only
> function apart from fork that returns twice.
After fork() all address values are still the same, including the
address of objects on the stack. A new thread on the other hand will
have a new, empty stack at a different location.
After fork() both the parent process and the child process can return
from the function that called fork(). A new thread on the other hand
cannot return beyond CreateThread or pthread_create.
--
Br�der, in die Tonne die Freiheit,
Br�der, ein Stoppschild davor.
Egal was die Schwarzen Verlangen
Rufen wir: Ja! Brav im Chor.
comp.programming.threads might be a good place for this question.
setjmp and longjmp are standard C, but fork and threads are not.
--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"