1 Star 0 Fork 11

刘荣福/microPython

forked from QuecPython/microPython 
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
thread_shared2.py 750 Bytes
Copy Edit Raw Blame History
chenchi authored 2021-06-03 16:44 +08:00 . first commit
# test capability for threads to access a shared mutable data structure
# (without contention because they access different parts of the structure)
#
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
import _thread
def foo(lst, i):
lst[i] += 1
def thread_entry(n, lst, idx):
for i in range(n):
foo(lst, idx)
with lock:
global n_finished
n_finished += 1
lock = _thread.allocate_lock()
n_thread = 2
n_finished = 0
# the shared data structure
lst = [0, 0]
# spawn threads
for i in range(n_thread):
_thread.start_new_thread(thread_entry, ((i + 1) * 10, lst, i))
# busy wait for threads to finish
while n_finished < n_thread:
pass
print(lst)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/oi-lrf/microPython.git
git@gitee.com:oi-lrf/microPython.git
oi-lrf
microPython
microPython
master

Search