Skip to content

A tiny implementation of semaphore that actually works for learning

Notifications You must be signed in to change notification settings

melyourhero/toy-semaphore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Toy Semaphore

A tiny implementation of semaphore that actually works, for learning.

Usage

git clone [email protected]:melyourhero/toy-semaphore.git

cd toy-semaphore

npm i

npm start

Then try to connect to server, using several curl connections

This example simply restrict permission to critical section for two user at time.

const semaphore = new Semaphore(2);

http.createServer(async (req: IncomingMessage, res: ServerResponse): Promise<void> => {
    const string = await semaphore.acquire((): Promise<string> =>
        new Promise((resolve) => setTimeout(() => resolve('Example string'), 5000)));

    res.end(string)
});

For testing:

npm run test

About

A tiny implementation of semaphore that actually works for learning

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published