Skip to content

Commit f37281c

Browse files
committed
ubuntu 12 Aug
1 parent ca63fca commit f37281c

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

System/Processes/thread_classes.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import threading
2+
3+
class Mythread(threading.Thread):
4+
def __init__(self, myId, count, mutex):
5+
self.myId = myId
6+
self.count = count
7+
self.mutex = mutex
8+
threading.Thread.__init__(self)
9+
10+
def run(self):
11+
for i in range(self.count):
12+
with self.mutex:
13+
print('[%s] => %s' % (self.myId, i))
14+
15+
stdoutmutex = threading.Lock()
16+
17+
threads = []
18+
19+
for i in range(3):
20+
thread = Mythread(i, 10, stdoutmutex)
21+
thread.start()
22+
threads.append(thread)
23+
24+
for thread in threads:
25+
thread.join()
26+
27+
print('Main thread exting.')
28+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import _thread as thread
2+
3+
stdoutmutex = thread.allocate_lock()
4+
exitmutexes = [thread.allocate_lock() for i in range(3)]
5+
6+
def counter(myId, count):
7+
for i in range(count):
8+
stdoutmutex.acquire()
9+
print('[%s] => %s' % (myId, i))
10+
stdoutmutex.release()
11+
12+
exitmutexes[myId].acquire()
13+
for mutex in exitmutexes:
14+
print(mutex.locked())
15+
16+
for i in range(3):
17+
thread.start_new_thread(counter, (i, 10))
18+
19+
for mutex in exitmutexes:
20+
while not mutex.locked(): pass
21+
22+
for mutex in exitmutexes:
23+
print(mutex.locked())
24+
25+
for mutex in exitmutexes:
26+
mutex.release()
27+
28+
for mutex in exitmutexes:
29+
print(mutex.locked())
30+
31+
32+
print('Main thread exiting!')
33+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import _thread as thread
2+
3+
stdoutmutex = thread.allocate_lock()
4+
5+
exitmutexes = [False] * 3
6+
7+
def counter(myId, count):
8+
for i in range(count):
9+
stdoutmutex.acquire()
10+
print('[%s] => %s' % (myId, i))
11+
stdoutmutex.release()
12+
13+
exitmutexes[myId] = True
14+
15+
for i in range(3):
16+
thread.start_new_thread(counter, (i, 10))
17+
18+
while False in exitmutexes: pass
19+
20+
print('Main thread exiting!')
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import _thread as thread
2+
import time
3+
4+
stdoutmutex = thread.allocate_lock()
5+
numthread = 5
6+
exitmutexes = [thread.allocate_lock() for i in range(numthread)]
7+
8+
def counter(myId, count, mutex):
9+
for i in range(count):
10+
time.sleep(1 / (myId + 1))
11+
with mutex:
12+
print('[%s] => %s' % (myId, i))
13+
exitmutexes[myId].acquire()
14+
15+
for i in range(numthread):
16+
thread.start_new_thread(counter, (i, 5, stdoutmutex))
17+
18+
while not all(mutex.locked() for mutex in exitmutexes): pass
19+
20+
print('Main thread exiting!')

0 commit comments

Comments
 (0)