CHECKOUT_LOCK_FILETrytoestablishcontrolofalockfile[FID,KEY]=CHECKOUT_LOCK_FILE(FILENAME)ThisfunctiontriestocheckoutthefileFILENAMEsothatdifferentprogramsdonotperformsomeoperationatthesametime.Thisisaquickanddirtysemaphoreimplementation(seeWikipediaifunfamilarwithsemaphores).TheLOCKFILEwillalsoEXPIREin1hourunlessotherwisespecifiedbelow.AKEYisreturned,whichisnecessarytopasstotoRELEASE_LOCK_FILE.ThisfunctiontriestocreateanemptyfilecalledFILENAME.IfthefileisNOTalreadypresentandthecreationsuccessful,thefilewillbecreatedandFIDwillreturnthefileID(seehelpfopen).Ifinsteadthefilealreadyexists,thefunctionwillcheckevery1secondfor30iterationstoseeifthefiledisappears.Ifthefunctionisneverabletocreateanewfilebecausetheoldfileexists,thenthefunctionwillgiveupandreturnFID<0.IMPORTANT:RESPONSIBLECLEANUP:ItisimportantthatiftheprogramthatcallsCHECKOUT_LOCK_FILEisabletocreatethefile(thatis,FID>0),thenitshouldcallRELEASE_LOCK_FILEtoremovethelockfile.IMPORANT:FILECLOSURE:If2outputargumentsaregiven(thatis,KEYisexamined),thenthelockfileisclosedbeforeCHECKOUT_LOCK_FILEexits.IfKEYisnotrequestedinoutput,thentheFIDisleftopenforbackwardscompatibility.Depricatedreleaseinstructions(newcodeshouldnotuse):1)closethefilewithfclose(FID)and2)deletethefileFILENAMEthatiscreatedwithdelete(FILENAME).IfFID<0,thenitshouldNOTdeletethefileFILENAMEbecauseitischeckedoutbysomeotherprogram.Thefunctioncanbecalledwithadditionaloutputarguments:[FID,KEY]=CHECKOUT_LOCK_FILE(FILENAME,CHECKLOOPS)Altersthenumberoftimesthefunctionwillcheck(at1secondintervals)toseeifFILENAMEhasdisappeared.[FID,KEY]=CHECKOUT_LOCK_FILE(FILENAME,CHECKLOOPS,THROWERROR)IfTHROWERRORis1,thefunctionwillreturnanerrorinsteadofreturningFID<0.[FID,KEY]=CHECKOUT_LOCK_FILE(FILENAME,CHECKLOOPS,THROWERROR,EXPIRATION_SECONDS)Thismodeallowsonetospecificallysettheexpirationtimeinseconds.CHECKOUT_LOCK_FILEwillexaminethefilefortheexpirationtimeandignoreandremovethelockfileifitis"expired".Bydefault,EXPIRATION_SECONDSis3600.Example:% I want to make sure only my program writes to myfile.txt.% All of my programs that write to myfile.txt will "check out" the % file by creating myfile.txt-lock.mylockfile=[userpathfilesep'myfile.txt-lock'];[lockfid,key]=checkout_lock_file(mylockfile);iflockfid>0,% do somethingrelease_lock_file(mylockfile,key);else,error(['Never got control of 'mylockfile'; it was busy.']);end;Seealso:RELEASE_LOCK_FILE,FOPEN,DELETE