9 ms·
I still don't quite understand what he means by a "fiber". Is this basically 160 blocks of memory allocated on the heap?
by warmwaffles 10y ago
I still don't quite understand what he means by a "fiber". Is this basically 160 blocks of memory allocated on the heap?
- Aeolos 10y agoFiber[1] is another term for coroutines. They are essentially an implementation of cooperative multithreading. [1] https://en.wikipedia.org/wiki/Fiber_%28computer_science%29 https://en.wikipedia.org/wiki/Fiber_%28computer_science%29
- warmwaffles 10y agoAny simple C libraries that demonstrate this? I'm rather curious.
- MarkSweep 10y agoSee libtask: https://swtch.com/libtask/ https://swtch.com/libtask/
- aidenn0 10y agoAnd mongrel2, which I hack on occasionally uses libtask to good effect for this. It's architecture is much simpler than this presentation though because mongrel2 is one thread per process, so inside the process there is only one fiber executing at any given time.
- corysama 10y agoThere's no official support for fibers in Posix. There are lots of implementations anyway, but it's hard to get completely right. Long ago, the Win32 team got tired of people rolling their own subtley incorrect/incomplete fiber implementations for Windows and added official support to put an end to that support issue https://msdn.microsoft.com/en-us/library/windows/desktop/ms682661.aspx https://msdn.microsoft.com/en-us/library/windows/desktop/ms6...
- btrask 10y agoActually, there is: https://en.wikipedia.org/wiki/Setcontext https://en.wikipedia.org/wiki/Setcontext A library I personally recommend, which supports several platforms and back-ends: http://byuu.org/library/libco/ http://byuu.org/library/libco/
- deleted 10y ago[deleted]
- sillysaurus3 10y agosetcontext isn't Posix, so corysama is the best kind of correct. But setcontext exists on most flavors of Linux, FreeBSD, and OS X, so you can basically assume it's supported.
- gpderetta 10y agosetcontext was posix until POSIX.2008.
- deleted 10y ago[deleted]
- david-given 10y agoA few years back I built an app which used coroutines extensively (<plug> greylisting SMTP proxy, now abandoned because I don't run my own mailserver any more, http://cowlark.com/spey/ http://cowlark.com/spey/ </plug>). I originally used getcontext/setcontext. Superficially it was really easy, but turned into a goddamned nightmare. The problem is that pthreads gets on really badly with user-allocated stacks; some pthreads implementations find the current thread ID by looking at a magic value on the current thread's stack, and of course with a user-allocated stack this wasn't there. But why was I using pthreads alongside coroutines, you ask? Well, I wasn't. But I was linking to a library which was referring to pthreads symbols, even though it wasn't calling them; which made the linker automatically pull in the thread-safe glibc, so malloc/free ended up wrapping in mutexes... The net effect was bizarre and infrequent crashes that only showed up on machines which weren't mine and that I didn't have access to. I spent months debugging that. Eventually I switched to simulating coroutines using pthreads with a big lock so that only one thread could run at a time. (Which actually turned out to be better, because now I could do some operations properly in parallel.) Beware of setcontext! Bewaaaaaare!
- notduncansmith 10y agoLibmill could be worth a look. https://github.com/sustrik/libmill https://github.com/sustrik/libmill
- deleted 10y ago[deleted]
- threeseed 10y agoBasically they are fine grained tasks that get executed by a pool of threads.
- corysama 10y agoA fiber is a thread in that it executes code and it has it's own stack, but it does not have premption. The OS won't thread-switch between fibers for you. You must manually switch in and out of executing it. Running in a fiber doesn't mean you've stopped using threads. A fiber runs in to context of a thread and that thread still has premption. But, inside the thread, you can call functions to switch stacks in order to manually pause and resume execution inside the fiber.
- warmwaffles 10y agoOh that actually makes a lot of sense
- 2bitencryption 10y agoI've noticed that lots of recent advances in threading are all just ways to avoid context switching of threads and instead handle those switches manually (e.g. "green threads" in Python).
- pvg 10y agoThese aren't particularly recent. Java originally had green threads; I think the term itself is from there.
- wavesplash 10y agoGreen threads were a thing long before Java.