Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
b7274e91
Commit
b7274e91
authored
9 years ago
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
tests/thread: Add test for concurrent mutating of user instance.
parent
2e4cdae4
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
tests/thread/mutate_instance.py
+43
-0
43 additions, 0 deletions
tests/thread/mutate_instance.py
with
43 additions
and
0 deletions
tests/thread/mutate_instance.py
0 → 100644
+
43
−
0
View file @
b7274e91
# test concurrent mutating access to a shared user instance
#
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
import
_thread
# the shared user class and instance
class
User
:
def
__init__
(
self
):
self
.
a
=
'
A
'
self
.
b
=
'
B
'
self
.
c
=
'
C
'
user
=
User
()
# main thread function
def
th
(
n
,
lo
,
hi
):
for
repeat
in
range
(
n
):
for
i
in
range
(
lo
,
hi
):
setattr
(
user
,
'
attr_%u
'
%
i
,
repeat
+
i
)
assert
getattr
(
user
,
'
attr_%u
'
%
i
)
==
repeat
+
i
with
lock
:
global
n_finished
n_finished
+=
1
lock
=
_thread
.
allocate_lock
()
n_repeat
=
30
n_range
=
300
n_thread
=
4
n_finished
=
0
# spawn threads
for
i
in
range
(
n_thread
):
_thread
.
start_new_thread
(
th
,
(
n_repeat
,
i
*
n_range
,
(
i
+
1
)
*
n_range
))
# busy wait for threads to finish
while
n_finished
<
n_thread
:
pass
# check user instance has correct contents
print
(
user
.
a
,
user
.
b
,
user
.
c
)
for
i
in
range
(
n_thread
*
n_range
):
assert
getattr
(
user
,
'
attr_%u
'
%
i
)
==
n_repeat
-
1
+
i
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment