www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to use Fiber?

reply "FrankLike" <1150015857 qq.com> writes:
There is a int[] ,how to use the Fiber execute it ?
Such as :

import std.stdio;
import core.thread;


class DerivedFiber : Fiber
{
     this()
     {
         super( &run );
     }

private :
     void run()
     {
         printf( "Derived fiber running.\n" );
         faa();
     }
}

int[] v;

  void ftread()
{
	DerivedFiber work = new DerivedFiber();
	writeln( " will call " );
	work.call();
	writeln( " stop call " );
}
void faa()
{
	writeln( " start " );
	//Fiber.yield();
	writeln( " start yield " );
     foreach(c;v)
     {
     	writeln( " current n is ",c );
     }
}
void main()
{
int n=1;
	while(n<=10_001)
	{
		v~=n;
		n+=5000;
	}
printf( "Execution returned to calling context.\n" );
   ftread();
}
-------------end------------

I dont's think it's a good work.
How about you?

Thank you.
Feb 24 2015
next sibling parent "Kagamin" <spam here.lot> writes:
Huh? If you wanted to print an array, then
---
void main()
{
int n=1;
	while(n<=10_001)
	{
		v~=n;
		n+=5000;
	}
     foreach(c;v)
     {
     	writeln( " current n is ",c );
     }
}
---
Feb 25 2015
prev sibling parent reply "Dejan Lekic" <dejan.lekic gmail.com> writes:
On Tuesday, 24 February 2015 at 10:15:29 UTC, FrankLike wrote:
 There is a int[] ,how to use the Fiber execute it ?
 Such as :

 import std.stdio;
 import core.thread;


 class DerivedFiber : Fiber
 {
     this()
     {
         super( &run );
     }

 private :
     void run()
     {
         printf( "Derived fiber running.\n" );
         faa();
     }
 }

 int[] v;

  void ftread()
 {
 	DerivedFiber work = new DerivedFiber();
 	writeln( " will call " );
 	work.call();
 	writeln( " stop call " );
 }
 void faa()
 {
 	writeln( " start " );
 	//Fiber.yield();
 	writeln( " start yield " );
     foreach(c;v)
     {
     	writeln( " current n is ",c );
     }
 }
 void main()
 {
 int n=1;
 	while(n<=10_001)
 	{
 		v~=n;
 		n+=5000;
 	}
 printf( "Execution returned to calling context.\n" );
   ftread();
 }
 -------------end------------

 I dont's think it's a good work.
 How about you?

 Thank you.
On the "Articles" page on D Wiki ( http://wiki.dlang.org/Articles ) you have this link: http://octarineparrot.com/article/view/getting-more-fiber-in-your-diet It is probably the best article about using fibers in D that I have seen so far.
Feb 25 2015
parent "FrankLike" <1150015857 qq.com> writes:
On Wednesday, 25 February 2015 at 14:47:37 UTC, Dejan Lekic wrote:

 On the "Articles" page on D Wiki ( 
 http://wiki.dlang.org/Articles ) you have this link: 
 http://octarineparrot.com/article/view/getting-more-fiber-in-your-diet

 It is probably the best article about using fibers in D that I 
 have seen so far.
Thank you,I mean concurrency. I've get answer by book 'programming in D'.
Feb 25 2015