4 ms·
I have found that thread contention can be a real problem if you get a little lazy in minimizing the area of contention. My own data management project (https:/
by didgetmaster 4y ago
I have found that thread contention can be a real problem if you get a little lazy in minimizing the area of contention. My own data management project (https://didgets.com/ https://didgets.com/) is highly threaded. On high core count CPUs, threads can greatly speed up data processing; but multiple threads working on the same piece of data have to be very careful around critical sections.
Two problems I have encountered are 1) Locking too big an area (i.e. holding a mutex over an entire function when you just have to protect a single line of code) and 2) spinning off too many threads. I check the hardware to know how many physical cores the CPU has before deciding how many threads to create.
Spinning off hundreds of threads when the CPU only can run 8 at a time often does not help and can even be counter-productive.
- dragontamer 4y ago> Spinning off hundreds of threads when the CPU only can run 8 at a time often does not help and can even be counter-productive. Depends on how many of those threads become blocked by I/O. If you spin 200 threads, but 192 of them are blocked by I/O (waiting for disk and/or ethernet), then those 8-remaining threads effectively use the 8-cores of your CPU. Old-school programmers would remember the days of 1-core CPUs, where you'd need to use a threads (or processes actually) to effectively utilize hard drives or your ethernet connections.
- didgetmaster 4y agoThere are certainly instances where a large number of threads blocked waiting for I/O can be beneficial. I was merely pointing out that this is not always the case and you have to understand how adding another thread can help vs just getting in the way. Even with Disk I/O, too many threads trying to read/write to a HDD at once can lead to performance problems as the heads are thrashing trying to access different parts of the disk. Too many programmers just spin off threads without understanding what the implications really are.