Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Shao Miller <sha0.miller@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: [I'm learning C]: Learning to use ucontext |
| Date | 2012-01-29 11:42 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <jg3spl$b9g$1@dont-email.me> (permalink) |
| References | <6113912.484.1327853172515.JavaMail.geo-discussion-forums@yqfa18> |
On 1/29/2012 11:06, Andrey Popp wrote:
> Hello, I started to experimenting with ucontext to implement basic cooperative multithreading and stuccoed with this piece of code:
>
As far as I know, 'ucontext.h' isn't standard C. I think that's worth
noting.
> #include<stdlib.h>
> #include<stdio.h>
> #include<ucontext.h>
>
> static ucontext_t routine_main;
> static ucontext_t routine1;
> static ucontext_t routine2;
>
> void runner(int n) {
> fprintf(stderr, "runner %d\n", n);
> while(1) {
> fprintf(stderr, "runner %d\n", n);
> if (n == 1)
> swapcontext(&routine1,&routine2);
> else
> swapcontext(&routine2,&routine1);
> }
> }
>
> int main(int argc, char **argv) {
> char st1c[8192];
> char st2c[8192];
>
> if (getcontext(&routine1) == -1)
> abort();
> routine1.uc_link =&routine_main;
> routine1.uc_stack.ss_sp = st1c;
> routine1.uc_stack.ss_size = sizeof st1c;
> makecontext(&routine1, (void(*)(void))runner, 1, 1);
>
> if (getcontext(&routine2) == -1)
> abort();
> routine2.uc_link =&routine_main;
> routine2.uc_stack.ss_sp = st2c;
> routine2.uc_stack.ss_size = sizeof st2c;
> makecontext(&routine1, (void(*)(void))runner, 1, 2);
>
Does the line immediately above include a typo? Did you mean to modify
the 'routine1' context or the 'routine2' context there?
> fprintf(stderr, "start executing\n");
> if (swapcontext(&routine_main,&routine1) == -1)
> abort();
> fprintf(stderr, "exiting\n");
> return 0;
> }
>
> which doesn't work the way it should — it just constantly prints "start executing", which means it doesn't change execution context from routeine_main to routine1 but instead from routine_main to routine_main.
That's unfortunate.
--
"The stationery store has moved. Aaargh!"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
[I'm learning C]: Learning to use ucontext Andrey Popp <8mayday@gmail.com> - 2012-01-29 08:06 -0800
Re: [I'm learning C]: Learning to use ucontext Shao Miller <sha0.miller@gmail.com> - 2012-01-29 11:42 -0500
Re: [I'm learning C]: Learning to use ucontext Barry Schwarz <schwarzb@dqel.com> - 2012-01-29 10:07 -0800
Re: [I'm learning C]: Learning to use ucontext Jens Gustedt <jens.gustedt@loria.fr> - 2012-01-30 20:38 +0100
Re: [I'm learning C]: Learning to use ucontext Uncle Steve <stevet810@gmail.com> - 2012-01-30 18:58 -0500
Re: [I'm learning C]: Learning to use ucontext Keith Thompson <kst-u@mib.org> - 2012-01-30 17:05 -0800
csiph-web