added locking in order to prevent multiple simultaneous calls to unmanaged lib