Implemented mutex

Added a mutex for safety to ensure exiting scope of main compile
function ensures all files closed and no data leak.