Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Andrey Popp <8mayday@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | [I'm learning C]: Learning to use ucontext |
| Date | 2012-01-29 08:06 -0800 |
| Organization | http://groups.google.com |
| Message-ID | <6113912.484.1327853172515.JavaMail.geo-discussion-forums@yqfa18> (permalink) |
Hello, I started to experimenting with ucontext to implement basic cooperative multithreading and stuccoed with this piece of code:
#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);
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.
Back to comp.lang.c | Previous | Next — 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