GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - onepointerT/posixSemaphore: A simple POSIX Semaphore Library

BranchesTags
Open more actions menu
 
 

Latest commit

 

History

7 Commits
7 Commits

Folders and files

NameName
Last commit message
Last commit date
lib
lib
 
 
.gitignore
.gitignore
 
 
.gitmodules
.gitmodules
 
 
BS_thread_pool.hpp
BS_thread_pool.hpp
 
 
CMakeLists.txt
CMakeLists.txt
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
functionqueue.h
functionqueue.h
 
 
main.c
main.c
 
 
mutex.c
mutex.c
 
 
mutex.h
mutex.h
 
 
parampack.c
parampack.c
 
 
parampack.h
parampack.h
 
 
psignals.c
psignals.c
 
 
psignals.h
psignals.h
 
 
psync.c
psync.c
 
 
psync.h
psync.h
 
 
ptime.c
ptime.c
 
 
ptime.h
ptime.h
 
 
resultenv.h
resultenv.h
 
 
scheduler.c
scheduler.c
 
 
scheduler.h
scheduler.h
 
 
semaphore.c
semaphore.c
 
 
semaphore.cpp
semaphore.cpp
 
 
semaphore.h
semaphore.h
 
 
semaphore.hpp
semaphore.hpp
 
 
semaphore_threaded.h
semaphore_threaded.h
 
 
semaphore_type.c
semaphore_type.c
 
 
semaphore_type.h
semaphore_type.h
 
 
systime.c
systime.c
 
 
systime.h
systime.h
 
 
threading.c
threading.c
 
 
threading.h
threading.h
 
 
threadingpool.h
threadingpool.h
 
 
threadmem.h
threadmem.h
 
 
truth.c
truth.c
 
 
truth.h
truth.h
 
 

Repository files navigation

Simple POSIX Semaphore for Windows

POSIX semaphores allow processes and threads to synchronize their actions. A semaphore is an integer whose value is never allowed to fall below zero. Two operations can be performed on semaphores: increment the semaphore value by one [sem_post]; and decrement the semaphore value by one [sem_wait]. If the value of a semaphore is currently zero, then a sem_wait operation will block until the value becomes greater than zero.

The semaphore.c & semaphore.h files provide replacement for POSIX semaphores on Windows XP or later. The provided sem_ functions do not behave identically to their POSIX equivalents, but they will work as drop-in replacements for most use cases.

Functions Provided

  • int sem_init (sem_t *sem, int pshared, unsigned int value);

  • int sem_wait (sem_t *sem);

  • int sem_trywait (sem_t *sem);

  • int sem_timedwait (sem_t *sem, const struct timespec *abs_timeout);

  • int sem_post (sem_t *sem);

  • int sem_getvalue (sem_t *sem, int *value);

  • int sem_destroy (sem_t *sem);

  • sem_t *sem_open (const char *name, int oflag, mode_t mode, unsigned int value);

  • int sem_close (sem_t *sem);

  • int sem_unlink (const char *name);

Example

#include "semaphore.h"

int main(int argc, char **argv) {
sem_t *sem = sem_open("example_semaphore", O_CREAT | O_EXCL, 0, 1);

/*check if semaphore failed*/
if (sem == SEM_FAILED) {
	printf("Failed to acquired semaphore\n");
	return -1;
	}

printf("About to enter critical code block 1\n");
sem_wait(sem);

/*Critical Code Block 1*/
printf("In critical code block 1\n");
Sleep(2000);
printf("Finished critical code block 1\n\n");
sem_post(sem);

/*Normal Block 1*/
printf("Normal code block 1\n");
printf("About to enter critical code block 1\n");
sem_wait(sem);

/*Critical Code Block 2*/
printf("In critical code block 2\n");
Sleep(2000);
printf("Finished critical code 2\n\n");
sem_post(sem);

/*Normal Block 2*/
printf("Normal code block 2\n\n");

/*clean up*/
sem_unlink("example_semaphore");
printf("exiting\n");
sem_close(sem);
return 0;
}

Requirements

There are no external requirements to use. Just add the two files (semaphore.c & semaphore.h) to your project and include the semaphore.h header in the source file(s) you wish to use semaphores.

About

A simple POSIX Semaphore Library

Resources

Readme
Apache-2.0 license
Activity

Stars

1 star

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.