define the worker thread to implement open, inner & outer loops