Does grouping threads by task improve performance?

This is kind of a lazy question on my part, since I gave up after trying only a few google terms.

But when I read about multi-threaded performance, I read about context switching being bad for performance since the cache is thrashed or whatever.

My question is, is the cache always rendered useless after a context switch? What about when the context being switched to shares much of the stack and maybe even data which was being processed in the previous context? Would grouping thread pools by task or category of task help performance because it increased this effect?

Or is the cache always flushed no matter what because of a possible (inherent?) switch of address space or something like that?

4

Is the cache always rendered useless after a context switch?

No, not at all.

Context switches are actually a concept of the software that runs on a CPU, not one of the CPU itself. When the software decides to do a context switch, the state of the CPU is saved somewhere convenient, a new or previously-saved state is loaded and execution jumps to the address where the switched-to process was interrupted. That’s a bit oversimplified, but the major point is that the CPU is just following whatever instructions it’s told to execute and doesn’t know or care that the effect of those instructions is a context switch.

The cache is the domain of the CPU, which has its own rules for what it keeps in which cache and when evictions or commits to other caches or RAM happen. Say you have a single-core, single-cache CPU running a supervisor and two processes. If the code and data for all three fit in the cache, all three could context switch among themselves forever without ever having to reach outside the processor. This is because what’s in the cache remains valid between time slices. The only reason RAM would ever have to be touched is to make data visible off the processor, such as for peripherals doing I/O.

What about when the context being switched to shares much of the stack and maybe even data which was being processed in the previous context?

Contexts may operate in the same code and data space if they’re threads of the same process. One thing they can’t share is a stack, so each gets its own and the stack pointer is changed during a context switch. If there is data in common that’s being used frequently enough, it will remain in the cache. This effect is leveraged when designing software for very high performance, but it has the drawback of depending on CPU-specific features.

Would grouping thread pools by task or category of task help performance because it increased this effect?

It can, depending on the machine architecture, the workload and how you manage it. You want to run the same context on the same core as often as possible because it increases the odds of a cache hit, and cache hits improve performance. That doesn’t mean misses are automatically bad, but the cost has to be balanced with that of waiting in line for the same core, which may have already evicted what the context was using to socket-wide cache that can be accessed just as quickly by other cores.

You see this sort of thing on a macro scale in NUMA systems, where operating systems will try to assign contexts to the same socket to avoid cache invalidation and data transfer between the sockets.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật