Thread Mutex lock

Source: Internet
Author: User
Tags mutex

#!/usr/bin/env python
# Encoding:utf-8
# DATE:2018/6/18
From threading import Thread, Lock
Import time

n = 100


Def task ():
Global n
Mutex.acquire () # Prevent 99 other threads from waiting here to ensure data security
TEMP = n
Time.sleep (0.1) # Start a thread, sleep 0.1 seconds enough for 100 threads to start all
n = temp-1 # 100 Threads all modified to 99
Mutex.release ()


if __name__ = = ' __main__ ':
Mutex = Lock ()
t_l = []
time1 = Time.time ()
For I in range (100): # 100 threads opened within a process
t = Thread (Target=task)
T_l.append (t)
T.start ()

For I in t_l:
T.join () # Ensure that the main thread is finished executing
Time2 = Time.time ()
Time3 = time2-time1
Print (' master ', N, time3) # Master 0 10.06277322769165


Thread Mutex lock

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.