-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreads.py
More file actions
45 lines (35 loc) · 838 Bytes
/
threads.py
File metadata and controls
45 lines (35 loc) · 838 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 18 14:59:50 2017
@author: César Gutiérrez
"""
import threading
from threading import Thread
shared_var = 0
lock = threading.Lock()
def func1():
count = 0
global shared_var
while count < 999999:
lock.acquire()
shared_var += 1
lock.release()
count += 1
def func2():
count = 0
global shared_var
while count < 1000000:
lock.acquire()
shared_var -= 1
lock.release()
count += 1
def main():
global shared_var
thread1 = Thread(target = func1, args = ())
thread2 = Thread(target = func2, args = ())
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print "shared_var = %d" % shared_var
main()