Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > setjmp/longjmp with threads

Reply
Thread Tools

setjmp/longjmp with threads

 
 
sturlamolden
Guest
Posts: n/a
 
      08-05-2009
Here is a short question, forgive me if it shows plain ignorance of C:

- 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
 
Reply With Quote
 
 
 
 
Tom St Denis
Guest
Posts: n/a
 
      08-05-2009
On Aug 5, 4:15*am, sturlamolden <(E-Mail Removed)> wrote:
> Here is a short question, forgive me if it shows plain ignorance of C:
>
> - 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.


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
 
Reply With Quote
 
 
 
 
Alexander Bartolich
Guest
Posts: n/a
 
      08-05-2009
sturlamolden wrote:
> Here is a short question, forgive me if it shows plain ignorance of C:
>
> - Can I setjmp in one thread and longjmp to the saved jmp_buf in
> another?


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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-05-2009
sturlamolden <(E-Mail Removed)> writes:
> Here is a short question, forgive me if it shows plain ignorance of C:
>
> - 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.


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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Threads - Get running threads Pedro Pinto Java 2 04-08-2008 11:44 PM
[new to threads] threads with UI and loop Une bévue Ruby 0 06-14-2006 10:22 AM
TB View, Threads, Threads with unread The Invisible Man Firefox 1 03-20-2006 02:09 AM
Standard Threads vs Weightless Threads yoda Python 2 08-01-2005 09:12 PM
threads without threads sindica@gmail.com C Programming 4 08-27-2004 09:25 PM



Advertisments