<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: coroutijne code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 29 Aug 2008 17:46:58 GMT</pubDate>
    <description>DZone Snippets: coroutijne code</description>
    <item>
      <title>libPortableCoroutine test</title>
      <link>http://snippets.dzone.com/posts/show/2565</link>
      <description>// A program to test libCoroutine at http://www.dekorte.com/projects/opensource/libCoroutine/&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include "Base.h"&lt;br /&gt;#include "Coro.h"&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;int g_which = 0;&lt;br /&gt;Coro *firstCoro, *secondCoro, *thirdCoro, *fourthCoro, *fifthCoro;&lt;br /&gt;&lt;br /&gt;void fifthTask(void *context)&lt;br /&gt;{&lt;br /&gt;	int num = 0;&lt;br /&gt;&lt;br /&gt;	printf("5th Task created with value %d\n", *(int *)context);&lt;br /&gt;	while (1) {&lt;br /&gt;		if (g_which != 5) {&lt;br /&gt;			printf("5 != g_which %d\n", g_which); fflush(stdout);&lt;br /&gt;			exit(1);&lt;br /&gt;		} else printf("OK");&lt;br /&gt;		printf("5th Task: %d %d\n", (int)Coro_bytesLeftOnStack(fifthCoro),  num++);&lt;br /&gt;		Coro_switchTo_(fifthCoro, firstCoro);&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;void fourthTask(void *context)&lt;br /&gt;{&lt;br /&gt;	int num = 0;&lt;br /&gt;&lt;br /&gt;	printf("4th Task created with value %d\n", *(int *)context);&lt;br /&gt;	while (1) {&lt;br /&gt;		if (g_which != 4) {&lt;br /&gt;			printf("4 != g_which %d\n", g_which); fflush(stdout);&lt;br /&gt;			exit(1);&lt;br /&gt;		} else printf("OK");&lt;br /&gt;		printf("4th Task: %d %d\n", (int)Coro_bytesLeftOnStack(fourthCoro),  num++);&lt;br /&gt;		Coro_switchTo_(fourthCoro, firstCoro);&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;void thirdTask(void *context)&lt;br /&gt;{&lt;br /&gt;	int num = 0;&lt;br /&gt;&lt;br /&gt;	printf("Third Task created with value %d\n", *(int *)context);&lt;br /&gt;	while (1) {&lt;br /&gt;		if (g_which != 3) {&lt;br /&gt;			printf("3 != g_which %d\n", g_which); fflush(stdout);&lt;br /&gt;			exit(1);&lt;br /&gt;		} else printf("OK");&lt;br /&gt;		printf("Third Task: %d %d\n", (int)Coro_bytesLeftOnStack(thirdCoro),  num++);&lt;br /&gt;		Coro_switchTo_(thirdCoro, firstCoro);&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void secondTask(void *context)&lt;br /&gt;{&lt;br /&gt;	int num = 0;&lt;br /&gt;	&lt;br /&gt;	printf("secondTask created with value %d\n", *(int *)context);&lt;br /&gt;	while (1) {&lt;br /&gt;		if (g_which != 2) {&lt;br /&gt;			printf("2 != g_which %d\n", g_which); fflush(stdout);&lt;br /&gt;			exit(1);&lt;br /&gt;		} else printf("OK");&lt;br /&gt;		printf("secondTask: %d %d\n", (int)Coro_bytesLeftOnStack(secondCoro),  num++);&lt;br /&gt;		Coro_switchTo_(secondCoro, firstCoro);&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void firstTask(void *context)&lt;br /&gt;{&lt;br /&gt;	int value = 2;&lt;br /&gt;	int num  = 0;&lt;br /&gt;	&lt;br /&gt;	secondCoro = Coro_new();&lt;br /&gt;	g_which = 2;&lt;br /&gt;	Coro_startCoro_(firstCoro, secondCoro, (void *)&amp;value, secondTask);&lt;br /&gt;&lt;br /&gt;	thirdCoro = Coro_new();&lt;br /&gt;	g_which = 3;&lt;br /&gt;	Coro_startCoro_(firstCoro, thirdCoro, (void *)&amp;value, thirdTask);&lt;br /&gt;&lt;br /&gt;	fourthCoro = Coro_new();&lt;br /&gt;	g_which = 4;&lt;br /&gt;	Coro_startCoro_(firstCoro, fourthCoro, (void *)&amp;value, fourthTask);&lt;br /&gt;&lt;br /&gt;	fifthCoro = Coro_new();&lt;br /&gt;	g_which = 5;&lt;br /&gt;	Coro_startCoro_(firstCoro, fifthCoro, (void *)&amp;value, fifthTask);&lt;br /&gt;&lt;br /&gt;	printf("firstTask created with value %d\n", *(int *)context);&lt;br /&gt;&lt;br /&gt;	while (1) {&lt;br /&gt;		printf("firstTask: %d %d\n", (int)Coro_bytesLeftOnStack(firstCoro), num++);&lt;br /&gt;		switch (num % 4) {&lt;br /&gt;		case 0:&lt;br /&gt;			g_which = 2;&lt;br /&gt;			Coro_switchTo_(firstCoro, secondCoro);&lt;br /&gt;			break;&lt;br /&gt;		case 1:&lt;br /&gt;			g_which = 3;&lt;br /&gt;			Coro_switchTo_(firstCoro, thirdCoro);&lt;br /&gt;			break;&lt;br /&gt;		case 2:&lt;br /&gt;			g_which = 4;&lt;br /&gt;			Coro_switchTo_(firstCoro, fourthCoro);&lt;br /&gt;			break;&lt;br /&gt;		case 3:&lt;br /&gt;			g_which = 5;&lt;br /&gt;			Coro_switchTo_(firstCoro, fifthCoro);&lt;br /&gt;			break;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;	Coro *mainCoro = Coro_new();&lt;br /&gt;	int value = 1;&lt;br /&gt;	&lt;br /&gt;	Coro_initializeMainCoro(mainCoro);&lt;br /&gt;	&lt;br /&gt;	firstCoro = Coro_new();&lt;br /&gt;	g_which = 1;&lt;br /&gt;	Coro_startCoro_(mainCoro, firstCoro, (void *)&amp;value, firstTask);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 08 Sep 2006 09:33:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2565</guid>
      <author>frontera000 (bob bae)</author>
    </item>
  </channel>
</rss>
