Fix multithreaded cuda select device

Note:
- The bug is not detectable on OSX.
- It is known to fail on Linux.