non sequential execution...

S

s88

Hi all:
Consider the follows code slide...
void execution(int i){ printf("%d\n",i);}
void add(){
execution(1);
execution(3);
}

void sub(){
execution(2);
execution(4);
}
Is it possible print the 1 2 3 4 in C?
If possible, how to make it?
PS: can't use pthread (or other thread lib)!!!
Thanx!!
 
M

Mike Wahler

s88 said:
Hi all:
Consider the follows code slide...
void execution(int i){ printf("%d\n",i);}
void add(){
execution(1);
execution(3);
}

void sub(){
execution(2);
execution(4);
}
Is it possible print the 1 2 3 4 in C?
If possible, how to make it?
PS: can't use pthread (or other thread lib)!!!
Thanx!!

#include <stdio.h>

void execution(int i)
{
printf("%d\n",i);
}

void add()
{
static int i = -1;
execution(i += 2);
}

void sub()
{
static int i = 0;
execution(i += 2);
}

int main()
{
int i = 0;

for(i = 0; i < 2; ++i)
add(), sub();

return 0;
}

-Mike
 
S

s88

thank for your answer!!

I found another way...

jmp_buf flag1,flag2;
void add(){
execution(1);
if(setjmp(flag1)){
execution(3);
longjmp(flag2,1);
}
}

void sub(){
execution(2);
if(setjmp(flag2)){
execution(4);
return;
}
longjmp(flag1,1);
}

But it seems a little bit complicated..
Does exist more elegant way?
 
F

Flash Gordon

s88 said:
thank for your answer!!

I found another way...

Please include enough context for people to know what you are talking
about. Most people do not use Google and there is absolutely no
guarantee that they will have seen earlier messages in the thread.
Search this group for "Google context" to find instructions on how to
post properly through Google.
jmp_buf flag1,flag2;
void add(){
execution(1);
if(setjmp(flag1)){
execution(3);
longjmp(flag2,1);
}
}

void sub(){
execution(2);
if(setjmp(flag2)){
execution(4);
return;
}
longjmp(flag1,1);
}

This is not valid C. You are only allowed to call longjmp with a buffer
for a function that has not yet terminated.
But it seems a little bit complicated..
Does exist more elegant way?

Why would you want to do this? If you can come up with a sensible
scenario in which you think this behaviour is required we can try to
come up with a sensible way of solving the problem.
 
S

s88

Sorry everybody, I didn't notice this reply context problem...
, and thank you Gordon.

okay, I'm trying to build up a multi-cores simulator in C.
And my boss does not allow me to use the lib such like pthread.
So, I'm searching a better way to solve this problem.

Dave.

Flash Gordon
 
K

Keith Thompson

s88 said:
Sorry everybody, I didn't notice this reply context problem...
, and thank you Gordon.

Great. Next lesson: top-posting. Your reply goes *below*, or
interspersed with, any quoted text, so the entire article can be read
from top to bottom. Also, it's not always necessary to quote the
entire original article.
okay, I'm trying to build up a multi-cores simulator in C.
And my boss does not allow me to use the lib such like pthread.
So, I'm searching a better way to solve this problem.

So you want to functions to execute more or less concurrently, but
you're not allowed to use an existing library that will do this for
you.

What is the reason for this restriction?
 
K

Keith Thompson

Keith Thompson said:
Great. Next lesson: top-posting. Your reply goes *below*, or
interspersed with, any quoted text, so the entire article can be read
from top to bottom. Also, it's not always necessary to quote the
entire original article.


So you want to functions to execute more or less concurrently, but
you're not allowed to use an existing library that will do this for
you.

What is the reason for this restriction?

One more thing: <http://en.wikipedia.org/wiki/Coroutine> looks like it
might have some useful information.
 
K

Kenny McCormack

Keith Thompson said:
One more thing: <http://en.wikipedia.org/wiki/Coroutine> looks like it
might have some useful information.

Now, now, Keith. You know better than to suggest wikipedia as a source of
information in this ng. As you well know, wikipedia has been discredited
as a reliable source by you and your fellow, so-called, "regulars".

Get with the program!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top