begin write thread function