Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
846c3224
Kaydet (Commit)
846c3224
authored
May 17, 1994
tarafından
Guido van Rossum
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Added semaphores; fix event.wait().
üst
48a69b70
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
4 deletions
+51
-4
sync.py
Demo/threads/sync.py
+51
-4
No files found.
Demo/threads/sync.py
Dosyayı görüntüle @
846c3224
...
@@ -4,6 +4,7 @@
...
@@ -4,6 +4,7 @@
# condition() # a POSIX-like condition-variable object
# condition() # a POSIX-like condition-variable object
# barrier(n) # an n-thread barrier
# barrier(n) # an n-thread barrier
# event() # an event object
# event() # an event object
# semaphore(n=1)# a semaphore object, with initial count n
#
#
# CONDITIONS
# CONDITIONS
#
#
...
@@ -35,7 +36,7 @@
...
@@ -35,7 +36,7 @@
# threads are .wait'ing, this is a nop.
# threads are .wait'ing, this is a nop.
#
#
# Note that if a thread does a .wait *while* a signal/broadcast is
# Note that if a thread does a .wait *while* a signal/broadcast is
# in progress, it's guaranteeed to block until a subsequen
c
t
# in progress, it's guaranteeed to block until a subsequent
# signal/broadcast.
# signal/broadcast.
#
#
# Secret feature: `broadcast' actually takes an integer argument,
# Secret feature: `broadcast' actually takes an integer argument,
...
@@ -184,8 +185,30 @@
...
@@ -184,8 +185,30 @@
# see the event get around to .wait'ing on it. But so long as you don't
# see the event get around to .wait'ing on it. But so long as you don't
# need to .clear an event, events are easy to use safely.
# need to .clear an event, events are easy to use safely.
#
#
# Tim Peters tim@ksr.com
# SEMAPHORES
# not speaking for Kendall Square Research Corp
#
# A semaphore object is created via
# import this_module
# your_semaphore = this_module.semaphore(count=1)
#
# A semaphore has an integer count associated with it. The initial value
# of the count is specified by the optional argument (which defaults to
# 1) passed to the semaphore constructor.
#
# Methods:
#
# .p()
# If the semaphore's count is greater than 0, decrements the count
# by 1 and returns.
# Else if the semaphore's count is 0, blocks the calling thread
# until a subsequent .v() increases the count. When that happens,
# the count will be decremented by 1 and the calling thread resumed.
#
# .v()
# Increments the semaphore's count by 1, and wakes up a thread (if
# any) blocked by a .p(). It's an (detected) error for a .v() to
# increase the semaphore's count to a value larger than the initial
# count.
import
thread
import
thread
...
@@ -306,10 +329,34 @@ class event:
...
@@ -306,10 +329,34 @@ class event:
def
wait
(
self
):
def
wait
(
self
):
self
.
posted
.
acquire
()
self
.
posted
.
acquire
()
while
not
self
.
state
:
if
not
self
.
state
:
self
.
posted
.
wait
()
self
.
posted
.
wait
()
self
.
posted
.
release
()
self
.
posted
.
release
()
class
semaphore
:
def
__init__
(
self
,
count
=
1
):
if
count
<=
0
:
raise
ValueError
,
'semaphore count
%
d; must be >= 1'
%
count
self
.
count
=
count
self
.
maxcount
=
count
self
.
nonzero
=
condition
()
def
p
(
self
):
self
.
nonzero
.
acquire
()
while
self
.
count
==
0
:
self
.
nonzero
.
wait
()
self
.
count
=
self
.
count
-
1
self
.
nonzero
.
release
()
def
v
(
self
):
self
.
nonzero
.
acquire
()
if
self
.
count
==
self
.
maxcount
:
raise
ValueError
,
'.v() tried to raise semaphore count above '
\
'initial value '
+
`maxcount`
self
.
count
=
self
.
count
+
1
self
.
nonzero
.
signal
()
self
.
nonzero
.
release
()
# The rest of the file is a test case, that runs a number of parallelized
# The rest of the file is a test case, that runs a number of parallelized
# quicksorts in parallel. If it works, you'll get about 600 lines of
# quicksorts in parallel. If it works, you'll get about 600 lines of
# tracing output, with a line like
# tracing output, with a line like
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment