Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions homework3/sync_condition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Homework 3: Thread Synchronization Using Conditions"""

from threading import Condition
from threading import Thread


def print_odd(cv):
"""Function to print odd numbers"""
nums = [x for x in range(101) if x % 2 == 1]
for num in nums:
with cv:
cv.wait()
print(num)
cv.notify()


def print_even(cv):
"""Function to print even numbers"""
nums = [x for x in range(101) if x % 2 == 0]
for num in nums:
with cv:
print(num)
cv.notify()
if (num != 100):
cv.wait()


if __name__ == '__main__':
condition = Condition()
odd = Thread(name='odd', target=print_odd, args=(condition,))
even = Thread(name='even', target=print_even, args=(condition,))

odd.start()
even.start()
42 changes: 42 additions & 0 deletions homework3/sync_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Homework 3: Thread Synchronization Using Events"""

from threading import Event
from threading import Thread


class EventThread(Thread):
"""My thread to work with events"""

main_event = Event()

def __init__(self, thread_name):
super().__init__()

self.thread_name = thread_name

if self.thread_name == 'even':
self.nums = [x for x in range(101) if x % 2 == 0]
else:
self.nums = [x for x in range(101) if x % 2 == 1]

def run(self):
"""Implementation of thread's activity"""

for num in self.nums:
if self.thread_name == 'even':
while self.main_event.isSet():
pass
print(num)
self.main_event.set()
else:
self.main_event.wait()
print(num)
self.main_event.clear()


if __name__ == '__main__':
even_thread = EventThread('even')
odd_thread = EventThread('odd')

odd_thread.start()
even_thread.start()
43 changes: 43 additions & 0 deletions homework3/sync_lock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Homework 3: Thread Synchronization Using Locks"""

from threading import Lock
from threading import Thread


class LockThread(Thread):
"""My thread to work with locks"""

even_lock = Lock()
odd_lock = Lock()

def __init__(self, thread_name):
super().__init__()

self.thread_name = thread_name

if self.thread_name == 'even':
self.nums = [x for x in range(101) if x % 2 == 0]
self.odd_lock.acquire()
else:
self.nums = [x for x in range(101) if x % 2 == 1]

def run(self):
"""Implementation of thread's activity"""

for num in self.nums:
if self.thread_name == 'even':
self.even_lock.acquire()
print(num)
self.odd_lock.release()
else:
self.odd_lock.acquire()
print(num)
self.even_lock.release()


if __name__ == '__main__':
even_thread = LockThread('even')
odd_thread = LockThread('odd')

odd_thread.start()
even_thread.start()
41 changes: 41 additions & 0 deletions homework3/sync_semaphore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Homework 3: Thread Synchronization Using Semaphores"""

from threading import Semaphore
from threading import Thread


class SemaphoreThread(Thread):
"""My thread to work with semaphores"""

even_sem = Semaphore(1)
odd_sem = Semaphore(0)

def __init__(self, thread_name):
super().__init__()

self.thread_name = thread_name
if self.thread_name == 'even':
self.nums = [x for x in range(101) if x % 2 == 0]
else:
self.nums = [x for x in range(101) if x % 2 == 1]

def run(self):
"""Implementation of thread's activity"""

for num in self.nums:
if self.thread_name == 'even':
self.even_sem.acquire()
print(num)
self.odd_sem.release()
else:
self.odd_sem.acquire()
print(num)
self.even_sem.release()


if __name__ == '__main__':
even_thread = SemaphoreThread('even')
odd_thread = SemaphoreThread('odd')

even_thread.start()
odd_thread.start()
24 changes: 24 additions & 0 deletions homework3/sync_timer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Homework 3: Thread Synchronization Using Timer"""

from threading import Timer
from time import sleep


def timer_thread(start_num, sleep_time):
for num in range(start_num, 101, 2):
print(num)
sleep(sleep_time)


if __name__ == '__main__':
sleep_time = 1
even_start_num = 0
odd_start_num = 1

even_thread = Timer(sleep_time / 2, timer_thread,
args=[even_start_num, sleep_time])
odd_thread = Timer(sleep_time, timer_thread,
args=[odd_start_num, sleep_time])

even_thread.start()
odd_thread.start()