|
1 | 1 | {
|
2 | 2 | "cells": [
|
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "https://www.pythontutorial.net/python-concurrency/python-threading-lock/" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "illustrates a race condition: Output will vary 10 & 20 each time you run , depending race" |
| 15 | + ] |
| 16 | + }, |
3 | 17 | {
|
4 | 18 | "cell_type": "code",
|
5 | 19 | "execution_count": null,
|
6 | 20 | "metadata": {},
|
7 | 21 | "outputs": [],
|
8 | 22 | "source": [
|
9 |
| - "import threading\n", |
| 23 | + "from threading import Thread\n", |
| 24 | + "from time import sleep\n", |
| 25 | + "\n", |
| 26 | + "\n", |
| 27 | + "counter = 0\n", |
| 28 | + "\n", |
| 29 | + "# define a function that increases the value of the counter variable by a number:\n", |
| 30 | + "def increase(by):\n", |
| 31 | + " global counter\n", |
10 | 32 | "\n",
|
11 |
| - "# Initialize the mutex lock\n", |
12 |
| - "mutex = threading.Lock()\n", |
| 33 | + " local_counter = counter\n", |
| 34 | + " local_counter += by\n", |
13 | 35 | "\n",
|
14 |
| - "# Function to demonstrate using the mutex lock\n", |
15 |
| - "def protected_critical_section(name):\n", |
16 |
| - " # Acquire the mutex lock to protect the critical section\n", |
17 |
| - " mutex.acquire()\n", |
18 |
| - " try:\n", |
19 |
| - " # Critical section code here\n", |
20 |
| - " print(f\"{name} is in the critical section.\")\n", |
21 |
| - " finally:\n", |
22 |
| - " # Release the mutex lock after the critical section is done\n", |
23 |
| - " mutex.release()\n", |
| 36 | + " sleep(0.1)\n", |
| 37 | + "\n", |
| 38 | + " counter = local_counter\n", |
| 39 | + " print(f'counter={counter}\\n')\n", |
| 40 | + "\n", |
| 41 | + "\n", |
| 42 | + "# create two threads. \n", |
| 43 | + "# first thread increases the counter by 10 \n", |
| 44 | + "# second thread increases the counter by 20:\n", |
| 45 | + "t1 = Thread(target=increase, args=(10,))\n", |
| 46 | + "t2 = Thread(target=increase, args=(20,))\n", |
| 47 | + "\n", |
| 48 | + "# start the threads\n", |
| 49 | + "t1.start()\n", |
| 50 | + "t2.start()\n", |
| 51 | + "\n", |
| 52 | + "\n", |
| 53 | + "# wait for the threads to complete\n", |
| 54 | + "t1.join()\n", |
| 55 | + "t2.join()\n", |
| 56 | + "\n", |
| 57 | + "\n", |
| 58 | + "print(f'The final counter is {counter}')" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "markdown", |
| 63 | + "metadata": {}, |
| 64 | + "source": [ |
| 65 | + "#### Using a threading lock to prevent the race condition\n", |
24 | 66 | "\n",
|
25 |
| - "# Create two threads to demonstrate using the mutex lock\n", |
26 |
| - "thread1 = threading.Thread(target=protected_critical_section, args=(\"Thread 1\",))\n", |
27 |
| - "thread2 = threading.Thread(target=protected_critical_section, args=(\"Thread 2\",))\n", |
| 67 | + "In Python, you can use the `Lock` class from the `threading` module to create a lock object:\n", |
| 68 | + "\n", |
| 69 | + "First, create an instance the Lock class:\n", |
| 70 | + "\n", |
| 71 | + "```python\n", |
| 72 | + "lock = Lock()\n", |
| 73 | + "```\n", |
| 74 | + "\n", |
| 75 | + "By default, the lock is unlocked until a thread acquires it.\n", |
| 76 | + "\n", |
| 77 | + "Second, acquire a lock by calling the acquire() method:\n", |
| 78 | + "\n", |
| 79 | + "```python\n", |
| 80 | + "lock.acquire()\n", |
| 81 | + "```\n", |
| 82 | + "\n", |
| 83 | + "Third, release the lock once the thread completes changing the shared variable:\n", |
| 84 | + "\n", |
| 85 | + "```python\n", |
| 86 | + "lock.release()\n", |
| 87 | + "```" |
| 88 | + ] |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "code", |
| 92 | + "execution_count": null, |
| 93 | + "metadata": {}, |
| 94 | + "outputs": [], |
| 95 | + "source": [ |
| 96 | + "from threading import Thread, Lock\n", |
| 97 | + "from time import sleep\n", |
| 98 | + "\n", |
| 99 | + "# Initialize the global counter variable\n", |
| 100 | + "counter = 0\n", |
| 101 | + "\n", |
| 102 | + "# Define the thread function to increase the counter by a given value using a lock\n", |
| 103 | + "def increase(by, lock):\n", |
| 104 | + " global counter\n", |
| 105 | + "\n", |
| 106 | + " # Acquire the lock to ensure exclusive access to the shared counter\n", |
| 107 | + " lock.acquire()\n", |
| 108 | + "\n", |
| 109 | + " # Create a local copy of the counter to perform the update\n", |
| 110 | + " local_counter = counter\n", |
| 111 | + " local_counter += by\n", |
| 112 | + "\n", |
| 113 | + " # Simulate some time-consuming work using sleep\n", |
| 114 | + " sleep(0.1)\n", |
| 115 | + "\n", |
| 116 | + " # Update the global counter with the new value\n", |
| 117 | + " counter = local_counter\n", |
| 118 | + " print(f'counter={counter}')\n", |
| 119 | + "\n", |
| 120 | + " # Release the lock to allow other threads to access the shared counter\n", |
| 121 | + " lock.release()\n", |
| 122 | + "\n", |
| 123 | + "# Create a Lock object to synchronize access to the shared counter\n", |
| 124 | + "lock = Lock()\n", |
| 125 | + "\n", |
| 126 | + "# Create two threads, each incrementing the counter by a different value\n", |
| 127 | + "t1 = Thread(target=increase, args=(10, lock))\n", |
| 128 | + "t2 = Thread(target=increase, args=(20, lock))\n", |
28 | 129 | "\n",
|
29 | 130 | "# Start the threads\n",
|
30 |
| - "thread1.start()\n", |
31 |
| - "thread2.start()\n", |
| 131 | + "t1.start()\n", |
| 132 | + "t2.start()\n", |
| 133 | + "\n", |
| 134 | + "# Wait for the threads to complete their execution\n", |
| 135 | + "t1.join()\n", |
| 136 | + "t2.join()\n", |
| 137 | + "\n", |
| 138 | + "# Print the final value of the counter\n", |
| 139 | + "print(f'The final counter is {counter}')\n" |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "markdown", |
| 144 | + "metadata": {}, |
| 145 | + "source": [ |
| 146 | + "It’s easier to use the lock object with the with statement to acquire and release the lock within a block of code:" |
| 147 | + ] |
| 148 | + }, |
| 149 | + { |
| 150 | + "cell_type": "code", |
| 151 | + "execution_count": null, |
| 152 | + "metadata": {}, |
| 153 | + "outputs": [], |
| 154 | + "source": [ |
| 155 | + "from threading import Thread, Lock\n", |
| 156 | + "from time import sleep\n", |
| 157 | + "\n", |
| 158 | + "counter = 0\n", |
| 159 | + "\n", |
| 160 | + "def increase(by, lock):\n", |
| 161 | + " global counter\n", |
| 162 | + "\n", |
| 163 | + " # Use the 'with' statement to acquire and release the lock automatically\n", |
| 164 | + " with lock:\n", |
| 165 | + " # Create a local copy of the counter to perform the update\n", |
| 166 | + " local_counter = counter\n", |
| 167 | + " local_counter += by\n", |
| 168 | + "\n", |
| 169 | + " # Simulate some time-consuming work using sleep\n", |
| 170 | + " sleep(0.1)\n", |
| 171 | + "\n", |
| 172 | + " # Update the global counter with the new value\n", |
| 173 | + " counter = local_counter\n", |
| 174 | + " print(f'counter={counter}')\n", |
| 175 | + "\n", |
| 176 | + "lock = Lock()\n", |
| 177 | + "\n", |
| 178 | + "# create threads\n", |
| 179 | + "t1 = Thread(target=increase, args=(10, lock))\n", |
| 180 | + "t2 = Thread(target=increase, args=(20, lock))\n", |
| 181 | + "\n", |
| 182 | + "# start the threads\n", |
| 183 | + "t1.start()\n", |
| 184 | + "t2.start()\n", |
| 185 | + "\n", |
| 186 | + "# wait for the threads to complete\n", |
| 187 | + "t1.join()\n", |
| 188 | + "t2.join()\n", |
| 189 | + "\n", |
| 190 | + "# Print the final value of the counter\n", |
| 191 | + "print(f'The final counter is {counter}')\n" |
| 192 | + ] |
| 193 | + }, |
| 194 | + { |
| 195 | + "cell_type": "markdown", |
| 196 | + "metadata": {}, |
| 197 | + "source": [ |
| 198 | + "### Alternative" |
| 199 | + ] |
| 200 | + }, |
| 201 | + { |
| 202 | + "cell_type": "markdown", |
| 203 | + "metadata": {}, |
| 204 | + "source": [ |
| 205 | + "Alternative code follows a more structured and object-oriented approach by encapsulating the shared counter and lock within a class. This approach is more modular and reusable, allowing you to easily create multiple instances of the Counter class with their own separate counters and locks, each capable of independent synchronization" |
| 206 | + ] |
| 207 | + }, |
| 208 | + { |
| 209 | + "cell_type": "code", |
| 210 | + "execution_count": null, |
| 211 | + "metadata": {}, |
| 212 | + "outputs": [], |
| 213 | + "source": [ |
| 214 | + "from threading import Thread, Lock\n", |
| 215 | + "from time import sleep\n", |
| 216 | + "\n", |
| 217 | + "# Define a class to encapsulate the shared counter and lock\n", |
| 218 | + "class Counter:\n", |
| 219 | + " def __init__(self):\n", |
| 220 | + " # Initialize the counter value to 0\n", |
| 221 | + " self.value = 0\n", |
| 222 | + " # Create a lock object to synchronize access to the counter\n", |
| 223 | + " self.lock = Lock()\n", |
| 224 | + "\n", |
| 225 | + " # Method to increase the counter by a specified value\n", |
| 226 | + " def increase(self, by):\n", |
| 227 | + " # Acquire the lock to ensure exclusive access to the shared counter\n", |
| 228 | + " with self.lock:\n", |
| 229 | + " # Create a local variable to perform the update operation\n", |
| 230 | + " current_value = self.value\n", |
| 231 | + " # Increment the local variable by the specified value\n", |
| 232 | + " current_value += by\n", |
| 233 | + "\n", |
| 234 | + " # Simulate some time-consuming work using sleep\n", |
| 235 | + " sleep(0.1)\n", |
| 236 | + "\n", |
| 237 | + " # Update the shared counter with the new value\n", |
| 238 | + " self.value = current_value\n", |
| 239 | + " # Print the updated value of the counter\n", |
| 240 | + " print(f'counter={self.value}')\n", |
| 241 | + "\n", |
| 242 | + "# Main function\n", |
| 243 | + "def main():\n", |
| 244 | + " # Create an instance of the Counter class to manage the shared counter\n", |
| 245 | + " counter = Counter()\n", |
| 246 | + "\n", |
| 247 | + " # Create two threads with different increment values\n", |
| 248 | + " t1 = Thread(target=counter.increase, args=(10, ))\n", |
| 249 | + " t2 = Thread(target=counter.increase, args=(20, ))\n", |
| 250 | + "\n", |
| 251 | + " # Start the threads\n", |
| 252 | + " t1.start()\n", |
| 253 | + " t2.start()\n", |
| 254 | + "\n", |
| 255 | + " # Wait for the threads to complete their work\n", |
| 256 | + " t1.join()\n", |
| 257 | + " t2.join()\n", |
32 | 258 | "\n",
|
33 |
| - "# Wait for both threads to finish\n", |
34 |
| - "thread1.join()\n", |
35 |
| - "thread2.join()\n", |
| 259 | + " # Print the final value of the counter after both threads have finished\n", |
| 260 | + " print(f'The final counter is {counter.value}')\n", |
36 | 261 | "\n",
|
37 |
| - "print(\"All threads finished.\")\n" |
| 262 | + "# Check if the script is run directly (not imported as a module)\n", |
| 263 | + "if __name__ == '__main__':\n", |
| 264 | + " # Call the main function to start the threads and perform the synchronization\n", |
| 265 | + " main()\n" |
38 | 266 | ]
|
39 | 267 | }
|
40 | 268 | ],
|
|
0 commit comments