4 ms·
Swift doesn’t have threads in the same sense that go has them. The language doesn’t have special constricts for creating threads; you use library calls to do th
by Someone 8y ago
Swift doesn’t have threads in the same sense that go has them. The language doesn’t have special constricts for creating threads; you use library calls to do that.
You typically either use Grand Central Dispatch, or directly make OS calls to create threads. In either case, you get way larger stack sizes, 512kB for secondary threads by default (https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/CreatingThreads/CreatingThreads.html https://developer.apple.com/library/archive/documentation/Co...)
Also, AFAIK, those stacks never grow. If you try to grow a thread’s stack too much, it’s end of game (for that thread, I think, but possibly even for the application)
Go’s smaller stacks allow for way more goroutines to exist in parallel, but as this article shows, if go has to grow a stack, things slow down.
- saagarjha 8y agoTrying to grow a stack too much will cause a segmentation fault, which is recoverable but usually not something you would want to do.
- masklinn 8y agoYou can't grow a C stack, it's pre-allocated and fixed size. Trying to use too much of a stack - beyond what's initially allocated — is why you're going to segfault (in the best case), as you'll be traipsing on unallocated addresses and hopefully unmapped pages.
- saagarjha 8y agoI'm using "grow" in the sense of moving the stack pointer down. But yes, you're correct, it's trying to access that memory that will cause a segmentation fault. And to address your first point, I'm sure there's hijinks you can perform with mmap to add a couple more pages if you so desire.
- masklinn 8y ago> I'm sure there's hijinks you can perform with mmap to add a couple more pages if you so desire. I don't think you can do that, but I think you'd just create a bigger stack when spawning your process / thread: the stack allocation is virtual (at least on unices, possibly not on Windows?) so on a 64b system it's unlikely to be very expensive, physical pages will get allocated on-demand as stack use increases. This could probably be tested / benched using pthread: pthread_attr_setstacksize lets you define the (virtual) stack size before spawning the process.
- Someone 8y agoYou can only do that if the new virtual memory address range isn’t used yet for something else. If so, you can almost just as well make the stack larger to start with. Certainly on 64-bit systems with their enormous address spaces, that’s probably the wise thing yo do. Go Can’t do that because it is designed for allowing to run thousands of short-lived threads, even on 32-bit systems.