Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
/semPublic

Package sem provides a way to use System V semaphores.

License

NotificationsYou must be signed in to change notification settings

hslam/sem

Repository files navigation

PkgGoDevBuild StatusGo Report CardLICENSE

Package sem provides a way to use System V semaphores.

Get started

Install

go get github.com/hslam/sem

Import

import "github.com/hslam/sem"

Usage

Example

package mainimport ("fmt""github.com/hslam/ftok""github.com/hslam/sem""time")funcmain() {key,err:=ftok.Ftok("/tmp",0x22)iferr!=nil {panic(err)}nsems:=1semid,err:=sem.Get(key,nsems,0666)iferr!=nil {semid,err=sem.Get(key,nsems,sem.IPC_CREAT|sem.IPC_EXCL|0666)iferr!=nil {panic(err)}defersem.Remove(semid)forsemnum:=0;semnum<nsems;semnum++ {_,err:=sem.SetValue(semid,semnum,1)iferr!=nil {panic(err)}}}semnum:=0ifcount,err:=sem.GetValue(semid,semnum);err!=nil {panic(err)}elseifcount==0 {fmt.Printf("%s semnum %d wait\n",time.Now().Format("15:04:05"),semnum)}ok,err:=sem.P(semid,semnum,sem.SEM_UNDO)iferr!=nil {panic(err)}fmt.Printf("%s semnum %d P %t\n",time.Now().Format("15:04:05"),semnum,ok)time.Sleep(time.Second*10)ok,err=sem.V(semid,semnum,sem.SEM_UNDO)iferr!=nil {panic(err)}fmt.Printf("%s semnum %d V %t\n",time.Now().Format("15:04:05"),semnum,ok)time.Sleep(time.Second*20)}

Output

$ go run main.go12:35:21 semnum 0 Ptrue12:35:31 semnum 0 Vtrue

In another terminal.

$ go run main.go12:35:25 semnum 0wait12:35:31 semnum 0 Ptrue12:35:41 semnum 0 Vtrue

License

This package is licensed under a MIT license (Copyright (c) 2020 Meng Huang)

Author

sem was written by Meng Huang.

About

Package sem provides a way to use System V semaphores.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp