Clean up find_idle_thread function and use atomic operations for dynamic thread count.