support lambdas in threads

@amw does this look good ?
I'll try processes next ...


In my PR I have changed this to use a separate mutex. `@index_mutex.synchronize {}` is less expensive than `Thread.exclusive`. Your implementation does not allow other threads to perform any work during index increment even if that work is unrelated to this variable.