The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.

semaphore

cran dev conda covr

The goal of semaphore is to enable synchronization of concurrent R processes.

Implements named semaphores from the Boost C++ library. Semaphores are managed by the operating system, which is responsible for ensuring that this integer value can be safely incremented or decremented by multiple processes. Processes can also wait (blocking) for the value to become non-zero.

Works cross-platform, including Windows, MacOS, and Linux.

Installation

# Install the latest stable version from CRAN:
install.packages("semaphore")

# Or the development version from GitHub:
install.packages("pak")
pak::pak("cmmr/semaphore")

Usage

library(semaphore)

s <- create_semaphore()
print(s)
#> [1] "uUkKpNMbTVgaborHG4rH"

increment_semaphore(s)

decrement_semaphore(s, wait = FALSE)
#> [1] TRUE
decrement_semaphore(s, wait = FALSE)
#> [1] FALSE

remove_semaphore(s)
#> [1] TRUE

Example: Producer/Consumer

Open two separate R sessions on the same machine.

Session 1 - Producer

library(semaphore)
s <- 'mySemaphore'

create_semaphore(s)

# enable session 2 to output 'unblocked!' three times
increment_semaphore(s)
increment_semaphore(s)
increment_semaphore(s)

remove_semaphore(s)

Session 2 - Consumer

library(semaphore)
s <- 'mySemaphore'

for (i in 1:3) {

  # Block until session 1 increments the semaphore
  decrement_semaphore(s)
  
  # Do some work
  message('unblocked!')
}

message('finished')

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.