From bbd7c733ba4bdbce0f67ff41791e6ea9ea94e3bd Mon Sep 17 00:00:00 2001
From: TYLER CARAZA-HARTER <tharter@cs544-tharter.cs.wisc.edu>
Date: Fri, 14 Feb 2025 10:08:07 -0600
Subject: [PATCH] lec 1 demo

---
 lec/11-threads/code/lec1.ipynb | 163 +++++++++++++++++++++++++++++++++
 1 file changed, 163 insertions(+)
 create mode 100644 lec/11-threads/code/lec1.ipynb

diff --git a/lec/11-threads/code/lec1.ipynb b/lec/11-threads/code/lec1.ipynb
new file mode 100644
index 0000000..ee90ef9
--- /dev/null
+++ b/lec/11-threads/code/lec1.ipynb
@@ -0,0 +1,163 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 14,
+   "id": "3b8d1ed9-9568-473e-bfa2-3b08f9e4587f",
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "hi from thread ID:hi from main thread, ID: 65\n",
+      " 125\n"
+     ]
+    }
+   ],
+   "source": [
+    "import threading\n",
+    "import time\n",
+    "\n",
+    "def task():\n",
+    "    print(\"hi from thread ID:\", threading.get_native_id())\n",
+    "\n",
+    "t = threading.Thread(target=task)\n",
+    "t.start()\n",
+    "print(\"hi from main thread, ID:\", threading.get_native_id())"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 28,
+   "id": "651dc205-4fdc-4054-be28-b19e2e798017",
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "1000000\n"
+     ]
+    }
+   ],
+   "source": [
+    "total = 0\n",
+    "\n",
+    "def task(count):\n",
+    "    global total\n",
+    "    for i in range(count):\n",
+    "        total += 1\n",
+    "\n",
+    "t = threading.Thread(target=task, args=[1_000_000])\n",
+    "t.start()\n",
+    "t.join() # wait until it exits\n",
+    "print(total)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 23,
+   "id": "5eb4a3e1-decf-4352-b61c-b10e53aec763",
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "1000"
+      ]
+     },
+     "execution_count": 23,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "total"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 33,
+   "id": "ceeb7904-ff2c-424c-bf7c-724d1b285e3b",
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "1084635"
+      ]
+     },
+     "execution_count": 33,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "total = 0\n",
+    "\n",
+    "def task(count):\n",
+    "    global total\n",
+    "    for i in range(count):\n",
+    "        total += 1\n",
+    "\n",
+    "t1 = threading.Thread(target=task, args=[1_000_000])\n",
+    "t1.start()\n",
+    "\n",
+    "t2 = threading.Thread(target=task, args=[1_000_000])\n",
+    "t2.start()\n",
+    "\n",
+    "t1.join()\n",
+    "t2.join()\n",
+    "\n",
+    "total"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 36,
+   "id": "48a5226c-6d63-4062-a2be-f7dd890c4dc1",
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "  0           RESUME                   0\n",
+      "\n",
+      "  1           LOAD_NAME                0 (total)\n",
+      "              LOAD_CONST               0 (1)\n",
+      "              BINARY_OP               13 (+=)\n",
+      "              STORE_NAME               0 (total)\n",
+      "              RETURN_CONST             1 (None)\n"
+     ]
+    }
+   ],
+   "source": [
+    "import dis\n",
+    "dis.dis(\"total += 1\")"
+   ]
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Python 3.13-nogil",
+   "language": "python",
+   "name": "python3.13-nogil"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 3
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython3",
+   "version": "3.13.2"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
-- 
GitLab