Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prepared by Oussama Jebbar

Similar presentations


Presentation on theme: "Prepared by Oussama Jebbar"— Presentation transcript:

1 Prepared by Oussama Jebbar
Threads COEN 346 Prepared by Oussama Jebbar

2 Outline General Context Threads Creation: Runnables
Threads Creation: Callables Synchronization

3 Threads The smallest execution unit Shared Memory They can Challenges
Execute a process (sometimes even periodically) Return values Challenges Communication (synchronous Vs asynchronous) Scheduling Control access to shared data

4 Threads Creation: Runnables
JAVA

5 Threads Creation: Callables
JAVA

6 Threads Creation Python
import threading #define a class for your thread class myThread (threading.Thread): #constructor def __init__(self): #put constructor logic here #run method that contains the thread routine def run(self): #put the logic of your routine here # Create new threads thread1 = myThread() thread2 = myThread() # Start new Threads thread1.start() thread2.start()

7 Mutexes in C++ Used to control access to shared data
To prevent data from being altered simultaneously To make sure all threads access the right data (variable holding the right value) Include <mutex> use methods such as: lock or try_lock to make sure only one thread accesses the block of code Use the method unlock to release it after the thread is finished

8 Mutex Example

9 Common Threads Control Utilities
join(): a method used to make a thread wait for another one to finish (Rendez-vous) detach(): used in C++ to tell a thread that it does not have to wait for another one Some frameworks and programming languages include suspend/resume utilities

10 Exercise Fork-join model is a multithreaded implementation of a divide and conquer algorithm in which the sub-problems can be solved independently In this exercise we will have to implement the Strassen algorithm for matrix multiplication in a fork-join model Strassen algorithm for matrix multiplication Let A and B two square matrices, and C is their product If A and B are not 2n * 2n matrices, fill the missing rows and columns with zeros to make them so Partition A and B into equally sized blocks of size 2n-1 * 2n-1 Calculate the following matrices M1 = (A11 + A22)*(B11 + B22) M2 = (A21 + A22)*B11 M3 = A11*(B12-B22) M4 = A22*(B21-B11) M5 = (A11+A12)*B22 M6 = (A21-A11)*(B11+B12) M7 = (A12-A22)*(B21+B22) Calculate the four blocks that compose the product C as follows C11 = M1 + M4 – M5 + M7 C12 = M3 + M5 C21 = M2 + M4 C22 = M1 – M2 + M3 + M6 Adjust the size of the resulting matrix (in case zeros were added in the first step of the algorithm) Q1: Propose a fork-join model to implement this algorithm Q2: What is the number of threads that the fork-join model you proposed use to calculate the product of two matrices of size n? Q3: Knowing that the machines you have in the lab can only run up to ten threads concurrently, can you propose a solution to make your implementation work with inputs of a big size

11 Hint! Decoupling function/method invocation from its execution
Allows for a better use of resources and reuse of threads (thread pooling) Active Object pattern


Download ppt "Prepared by Oussama Jebbar"

Similar presentations


Ads by Google