create_semaphore {semaphore}R Documentation

Shared Memory Atomic Operations

Description

A semaphore is an integer that the operating system keeps track of. Any process that knows the semaphore's identifier can increment or decrement its value, though it cannot be decremented below zero.

When the semaphore is zero, calling decrement_semaphore(wait = FALSE) will return FALSE whereas decrement_semaphore(wait = TRUE) will block until the semaphore is incremented by another process. If multiple processes are blocked, a single call to increment_semaphore() will only unblock one of the blocked processes.

Usage

create_semaphore(id = NULL, value = 0, cleanup = TRUE)

increment_semaphore(id)

decrement_semaphore(id, wait = TRUE)

remove_semaphore(id)

Arguments

id

A semaphore identifier (string). create_semaphore() defaults to generating a random identifier.

value

The initial value of the semaphore.

cleanup

Remove the semaphore when R session exits.

wait

If TRUE, blocks until semaphore is greater than zero.

Value

Examples


    library(semaphore) 
    
    s <- create_semaphore()
    print(s)
    
    increment_semaphore(s)
    decrement_semaphore(s, wait = FALSE)
    decrement_semaphore(s, wait = FALSE)
    
    remove_semaphore(s)

[Package semaphore version 1.0.1 Index]