Fibers

Graham St Jack Graham.StJack at internode.on.net
Thu Oct 23 23:21:56 PDT 2008


On Wed, 22 Oct 2008 02:51:00 +0000, dsimcha wrote:

> I noticed that, in D 2.20, we now have an implementation of fibers for
> D2.  I understand the basic differences between a fiber and a thread
> (preemptive vs. cooperative multitasking, threads can take advantage of
> multicore, fibers can't).  However, from a more practical point of view,
> I've become curious, what are fibers actually good for?  What can be
> done with fibers that either can't be done at all or can't be done as
> efficiently/elegantly/safely with threads?

I'm having a lot of success using fibers since I found out about them 
recently. Traditionally I use a lot of threads in my applications, 
usually for one of these reasons:
* Desire to exploit multiple processors.
* Need to keep complex state on the stack, usually for object I/O
  with things like sockets, pipes and serial ports.

The last reason can be addressed with fibers, usually in conjunction with 
select, and I am finding that the resultant code complexity is a LOT 
lower because I don't have nearly as much thread-safety to worry about. 
The performance is a lot higher too because all those context switches 
have been replaced with fiber switches and select calls.



More information about the Digitalmars-d mailing list