LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-27-2014, 11:18 PM   #1
zerop
Member
 
Registered: Jul 2014
Posts: 65

Rep: Reputation: Disabled
Question how to use coroutine in c language


http://www.chiark.greenend.org.uk/~s...oroutines.html

compile the code in above link directly

main.c:6:0: warning: "crReturn" redefined [enabled by default]
main.c:4:0: note: this is the location of the previous definition
main.c: In function ‘function’:
main.c:12:22: error: macro "crReturn" passed 2 arguments, but takes just 1
main.c:12:9: error: ‘crReturn’ undeclared (first use in this function)
main.c:12:9: note: each undeclared identifier is reported only once for each function it appears in
main.c: In function ‘main’:
main.c:19:6: error: ‘for’ loop initial declarations are only allowed in C99 mode
main.c:19:6: note: use option -std=c99 or -std=gnu99 to compile your code
main.c:20:19: error: expected expression before ‘do’

Code:
#include <stdio.h>

#define crBegin static int state=0; switch(state) { case 0:
#define crReturn(i,x) do { state=i; return x; case i:; } while (0)
#define crFinish }
#define crReturn(x) do { state=__LINE__; return x; \
                         case __LINE__:; } while (0)
int function(void) {
    static int i;
    crBegin;
    for (i = 0; i < 10; i++)
        crReturn(1, i);
    crFinish;
}

int main(void)
{
    crBegin;
        for(int c= 0; c<3; ++c)
           printf("%d", crReturn(c));
        crReturn(EOF);
        crFinish;
    return 0;
}
 
Old 07-28-2014, 06:56 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Also from that link you found:

Code:
Of course, this trick violates every coding standard in the book. Try doing this in
your company's code and you will probably be subject to a stern telling off if not
disciplinary action! You have embedded unmatched braces in macros, used case within
sub-blocks, and as for the crReturn macro with its terrifyingly disruptive contents.
. . It's a wonder you haven't been fired on the spot for such irresponsible coding
practice. You should be ashamed of yourself.
Yeah, the writer their self acknowledges that pretty much EVERYONE will lambaste them for writing or using that code.

I didn't take the time to read it all, I fast glanced.

It was written in 2000.

My statements on this are: K.I.S.S. "Keep It Simple, Stupid!"

There is NO, strike that "ZERO" point coding that way. I recommend you don't do that.
 
Old 07-28-2014, 10:16 PM   #3
zerop
Member
 
Registered: Jul 2014
Posts: 65

Original Poster
Rep: Reputation: Disabled
what is zero point coding

why not do this in device module?

this is the fastest reading file without blocking
 
Old 07-29-2014, 08:04 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
You can write the original code and don't need to use all the preprocessor tricks that's merely so the code can look in the author's opinion more read-able. I do not agree.

When I say Zero point I mean there is "no value" in taking the original code and modifying it the way they have done.
 
Old 07-29-2014, 09:33 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
What are you trying to accomplish?

If you have few and simple situations that conceptually call for co-routine structure, it is simpler to just explicitly manage the state yourself. It would be easier and more maintainable if the language had direct co-routine support, but it doesn't. So don't try to fake it.

If you have many and medium complexity situations that conceptually call for co-routine, AND have serious performance constraints, AND cannot include a little ASM code, then a macro kludge such as in that article could be correct. But obviously you would need to understand the usage, which your post demonstrates you do not.

But that combination of conditions (needed to make such a kludge appropriate) is unlikely.

Long ago, I hit many situations with multiple complex requirements for co-routines and strict performance requirements. My answer was ASM code to switch stacks, so that real co-routines could be used in C or C++. That avoids all the kludges of those macros and covers more complicated situations more efficiently and totally destroys portability.

There is a very narrow range of complexity for which managing the state variables explicitly is too messy but that macro kludge is workable. In more complex cases, the macro kludge doesn't work. In less complex cases the macro kludge is no more readable/maintainable than explicit state variables.

In modern programming, anyone with a non trivial requirement for co-routines uses multiple threads instead. Multi threads is less efficient than a kludge, which in turn is less efficient than real co-routines. Multi threads takes a certain learning curve (which is a good idea anyway) and a certain coding overhead to getting started. But despite those disadvantages, multi-threading is the popular and well supported answer for problems that conceptually call for co-routines.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to change gtkspell language without changing Slackware language? André0991 Slackware 4 03-05-2014 03:09 PM
Is Assembly Language considered a Structured Language? theKbStockpiler Programming 4 01-30-2011 09:09 AM
[SOLVED] Can the language of fedora boot message change to other language, zh_CN,e.g.? jimtony Fedora 2 01-11-2011 03:03 AM
can't differentiate system language from keyboard language? lilou_b Linux - Newbie 3 04-14-2010 05:47 PM
Good linux chinese language language program? darsunt Linux - Software 1 04-10-2009 12:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration