Boost logo

Boost Users :

From: Zeljko Vrba (zvrba_at_[hidden])
Date: 2008-08-09 10:23:39


On Sat, Aug 09, 2008 at 03:07:46PM +0300, dizzy wrote:
>
> - if this weren't C++ but C then we could use setjmp/longjmp to transparently
> (to the user) simulate synchronous operation while asynchronous operation is
> done behind the scenes; the user writes code such as:
>
What you describe are coroutines. setjmp/longjmp are unsuitable for context
switching. On UNIX you have swapcontext() function which does it properly
(no problems with exceptions), and on Win32 you have the Fiber API. The
downside is that you have to allocate a stack for each coroutine, so you have
the same space overheads as with threads. (though, it is not hard to make
user-level context switching and scheduling more efficient than the OS's)

What I would recommend you to do is to transform your program into a
producer-consumer pipeline: have N worker threads, each of which is written in
a synchronous style, and hand them off work via queues. You can keep a global
count of active (non-sleeping) workers, and adjust concurrency level on the
fly.

You have stumbled upon a hot topic (events vs. threads), you can read f.ex.:
http://portal.acm.org/citation.cfm?id=1251058
http://portal.acm.org/citation.cfm?doid=502059.502057

(the latter paper might give you an idea about possible architecture
for your code, along the lines of the paragraph above)

Just out of curiosity, why are you rewriting thread-based code into
event-based code? It might be easier/cheaper to port[*] it instead to
an OS that handles large number of threads better..

[*] And if you're developing for UNIX, porting might be just a recompile.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net