critical section now available in http working threads
// example: prevent multiple threads to modify file content in the same time
const App = require('@unitybase/ub').App
// critical sectin must be registered once in the moment modules are evaluated
const MY_CS = App.registerCriticalSection('SHARED_FILE_ACCESS')
function concurrentFileAccess() {
// protect simulatious file access from the different threads
App.enterCriticalSection(FSSTORAGE_CS)
try {
const data = fs.readfileSync('/tmp/concurent', 'utf8')
// do some operation what modify data
fs.readfileSync('/tmp/concurent', data)
} finally {
// important to leave critical section in finally block to prevent forever lock
App.leaveCriticalSection(FSSTORAGE_CS)
}
}
Edited by Pavlo Mashliakovskiy