The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 18, 2019, 06:57:48 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: coroutines in C  (Read 5974 times)
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« on: March 24, 2008, 04:17:04 AM »

http://goron.de/~froese/coro/coro.html
I have not tried this one, just for reference

scheme has continuations, which you can use to freeze/restart a state of a thread.
(a little more complex then that but idea)

Anyway if someone was smart, use coro, then use multi curl with it.
then u could avoid complexity Wink
Logged
dink
Expert
****
Offline Offline

Posts: 349


View Profile
« Reply #1 on: March 24, 2008, 12:38:38 PM »

Gotta reply to this thread, Nop.  That way I can find it again later.  Thnx
Logged

[quote Nutballs]
the universe has a giant fist, and its got enough whoop ass for everyone.
[/quote]
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« Reply #2 on: March 24, 2008, 03:00:39 PM »

I was tired when i wrote it.
Anyway if you are doing PHP/Perl/Python etc you would have to make a wrapper.
(if those languages do something crazy with the stack you are in big trouble and i suspect that it will not work or something nasty will happen Smiley)
Also if you use this and native threads combined that nasty will happen.

I think (don't quote me on this) that it uses the same technique that chicken scheme uses.
chicken scheme is a scheme to C compiler, but its calls never return (so args stay allocated on the stack)
(scheme calls never return, hence that is how u can do tail recursion and shit like that).
chicken scheme is extremely fast, and in some cases when it comes to recursion can outperform hand written C code.
(how the fuk it works to be honest i am not 100% sure, read the fuking paper on how it works and if ur head does not blow up)

Python and ruby have continuations, but in ruby very slow, since they are saving all local vars on before they go into co-routine/continuation.

Anyway make wrapper for PHP/Perl/Python (not that hard in that case, u could probably just run the fucker thru swig).
then away u go.
I suspect should be no problem since someone make binding for SBCL (a lisp) and he have no problem.
If you are doing python (i know must be same shit for PHP etc) make sure you lock and unlock the global interpreter lock or u get nasty, (probably could get away with not doing this).

Using scheme and this method you can have 20/40 sockets open on a single thread, and ur shitbox vps can outperform a dedicated server.
Logged
dink
Expert
****
Offline Offline

Posts: 349


View Profile
« Reply #3 on: March 24, 2008, 04:12:44 PM »

Ya.  I still have that linky you gave me for chicken.

I downloaded the PDF, but I'm just fukin slow at
doing things like this.
Logged

[quote Nutballs]
the universe has a giant fist, and its got enough whoop ass for everyone.
[/quote]
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« Reply #4 on: March 24, 2008, 04:24:05 PM »

lisp/scheme is very hard to wrap your head arround.

Most people who start to use these languages, do not start using them until they are "old" as in have a lot of programming experience behind them.
Biggest reason is that to take advantage of the language, you have to know what the regular languages lack.

Things like continuations for a newbie are useless http://en.wikipedia.org/wiki/Continuation
And it took me like 6 months after i read about them to figure out a use for them.
Macros to rewrite code again are pretty much useless for a newbie. If he does not know how to make code to begin with, how can he make a macro that can rewrite it ?

Probably there are just as many libraries if not more for lisp then most other languages. But again they are hard for newbies to understand. Hard to find etc.
And a newbie can not really harness the power of scheme/lisp

Anyway i am playing arround with continuations, and on my travels found the library and thought maybe might be useful for someone.

Logged
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #5 on: March 24, 2008, 07:04:07 PM »

Hey NOP,

This is a form of data persistence right? In PHP couldn't this be acheived through APC and to a lesser (slower) extent session variables and serialize/unserialize?

In C/C++ I imagine you could accomplish the same sort of thing using a master thread or app. and communicating with it using some sort of IPC or system wide mutexes/semaphores, etc. ?

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« Reply #6 on: March 24, 2008, 07:42:53 PM »

@darkness the whole point is to avoid threads. Probably it can be done the way u say, but it will be messy, prown to bug etc.
Anyway in a nutshell i will explain i will gloss over some of the details. But i will try and explain how continuation work.

simplest place to start is with python generators
Code:
>>> def foo() :
...     yield 1
...     yield 2
...     yield 3
...     
>>> foo
<function foo at 0x84d5f44>

a=foo()
a
<generator object at 0x84dd2cc>

a.next()
1
a.next()
2
you have generator called foo
when i construct it and assign it to a
when i call a.next() it gives 1 , 2 etc
what is happening every time i next it runs it till next yield, and return that value in yield. when it hits yield it saves current state.

i will now show how done in scheme (u would not use this in real life, just shown like this for demo purpose)

Code:
(define cc '()) <--- cc is the state

(define (foo)
    (let ((i 0))
      (call/cc
       (lambda (k) <---- k is the continuation
         (set! cc k))) <--- save value of k in cc
      (set! i (+ i 7))
      i))

I load the above code into scheme
foo it the function everytime it start it take i and adds 7 to it. but i is local inside the function.
Code:
#;2> cc
() <--- cc has no value () is nil
#;3> (foo)
7 call foo 7 is returned
#;4> cc
#<procedure (a7138 . results798)> <-- notice value of cc now is a procedure this is the continuation
#;5> (cc '()) <--- call cc with a null arguement (using macros i can get arround this, but want to keep simple, a continuation always takes 1 arg)
14
#;6> (cc '())
21
#;7> (cc '())
28
#;8> (cc '())
35
and it counts
using a macro i can make the code look exactly python (well with brackets ofcourse Smiley)
but it go deeper then that.
I can pass the continuation back and forth etc.

in my case i am using it to emulate a statemachine.
so u will have a statemachine, but it will appear to be linear.
Logged
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #7 on: March 25, 2008, 01:19:17 AM »

So in a nutshell the advantages are:

1. It's fast

2. It's using local variables, not globals.

3. it's using the stack.

So the primary advantage over instantiating an object and maintaining state in that (with an equivalent call to a method) is speed?

A state machine can be written without coroutines/continuations so I guess I need to understand what the real advantage is? Sorry if I'm a bit slow on the uptake brother.

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« Reply #8 on: March 25, 2008, 05:29:45 AM »

Basically what you said, and most importantly simplicity.
Probably a hand written statemachine would be quicker (if done in C/C++)
With a statemachine you have to take care of all the book keeping urself.

Lets say you had a function that searches for a element in a tree.
when it finds the element you want it returns.

But lets say you want to search for another element in tree from where u leave off .....
normally you would have to save the state before u search again.
But if u used a continuation, you would just call the function again, and it would resume from where it leave off.

They are hard to wrap ur head arround. After i read about them, it took me like 6 months to figger out what they are good for.
After they where invented it took like 10 years to find a use for them Smiley

This article explains how they can be used in web framework
http://www.defmacro.org/ramblings/continuations-web.html


Logged
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #9 on: March 25, 2008, 05:35:21 PM »

Basically what you said, and most importantly simplicity.
Probably a hand written statemachine would be quicker (if done in C/C++)
With a statemachine you have to take care of all the book keeping urself.


This is what I would use for a state machine, just a personal preference, not forcing it down anyones throat :-)

http://www.boost.org/libs/statechart/doc/index.html


Lets say you had a function that searches for a element in a tree.
when it finds the element you want it returns.

But lets say you want to search for another element in tree from where u leave off .....
normally you would have to save the state before u search again.
But if u used a continuation, you would just call the function again, and it would resume from where it leave off.

OK, I think that I'm beginning to grok this, thanks.

This article explains how they can be used in web framework
http://www.defmacro.org/ramblings/continuations-web.html


Thanks, I'll have a read.

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« Reply #10 on: March 25, 2008, 06:29:05 PM »

statemachines are great and in some cases you should use them (classic example is calculator)
where i am using continuations in is with multicurl.

before i used a statemachine

so i would have to do something like this

get-google: State1
get(http://www.google.com)
push curl onto multicurl

get-google-result: do some shit with the result


using continuations you can make it appear linear.
get(http://www.google.com)
perform <--- this would be magic function that contains continuation adds it to multi etc
then when it returns do your shit

that way you have like 1/2 the ammount of code, and each part appears to be like a seperate thread

Logged
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #11 on: March 25, 2008, 08:05:07 PM »

I fully understand where you are coming from now dude, interesting....

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!