Skip to content

Instantly share code, notes, and snippets.

@whitespy
Created May 6, 2022 16:57
Show Gist options
  • Select an option

  • Save whitespy/4862184fa2b1bc3d837ca36604f265e6 to your computer and use it in GitHub Desktop.

Select an option

Save whitespy/4862184fa2b1bc3d837ca36604f265e6 to your computer and use it in GitHub Desktop.
{
"uuid": "941a4e17-eaa8-4f0e-a4ef-39c502eeb6b2",
"type": "code",
"subtype": "autograded_answer",
"points": null,
"source": [
"import threading\n",
"\n",
"### BEGIN SOLUTION\n",
"a = 0\n",
"threads = []\n",
"### END SOLUTION\n",
"\n",
"def x():\n",
" global a\n",
" for i in range(100_000):\n",
" a += 1\n",
"\n",
"for j in range(10):\n",
" thread = threading.Thread(target=x)\n",
" threads.append(thread)\n",
" thread.start()\n",
"\n",
"### BEGIN SOLUTION\n",
"for thread in threads:\n",
" thread.join()\n",
"### END SOLUTION\n",
"\n"
],
"outputs": null,
"tests_cells": [
{
"uuid": "e89d559f-69d4-4834-8053-859183c7e9bc",
"type": "code",
"subtype": "autograder_tests",
"points": 10,
"source": [
"### BEGIN HIDDEN TESTS\n",
"assert a == 1_000_000\n",
"### END HIDDEN TESTS\n",
"assert 1 == 1\n"
],
"outputs": null
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment