backport of a thread bugfix from 2_5_X branch Daniel * threads.c: backport of a thread bugfix from 2_5_X branch Daniel