Skip to content

Navigation Menu

Sign in
Appearance settings

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
Appearance settings

Commit e404a37

Browse files
Create synchronization_in_threads.py
1 parent 71bac47 commit e404a37

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
3+
import threading
4+
import time
5+
# global variable x
6+
x = 0
7+
8+
def Tfunc(i,lock):
9+
lock.acquire()
10+
print('from thread %d' %i)
11+
lock.release()
12+
13+
14+
# creating a lock
15+
lock = threading.Lock()
16+
17+
# creating threads
18+
t1 = threading.Thread(target=Tfunc, args=(1,lock,))
19+
t2 = threading.Thread(target=Tfunc, args=(2,lock,))
20+
21+
# start threads
22+
t1.start()
23+
t2.start()
24+
25+
# wait until threads finish their job
26+
t1.join()
27+
time.sleep(5)
28+
t2.join()
29+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /