MultithreadingLab
his lab will familiarize you with multithreading. You will implement switching between threads in a user-level threads package, use multiple threads to speed up a program, and implement a barrier.
Uthread: switching between threads
The Thread 0 is the main function , which will not be scheduled again after scheduled out .
Similar to the sched and schedule function , in uthread.c ,the ra and sp and other registers will be store and restore other thread’s registers in thread_schedule function (which is coded by asm ),so once the creation of the thread , the ra will be corresponding function , sp will the top of their own stack .
Once every thread is created, thread_schedule, the three thread will continue to switching!
In thread_schedule ,store and restore!
1 | thread_switch: |
1 | thread_switch((uint64)t,(uint64)current_thread); |
Creation of thread
1 | void |
This is different from the thread switching in the kernel . In this case ,we switch between different threads directly. In kernel , we switch to the CPU’s schedule thread ,and pick a thread to switch to.
Using threads
We will create a lock for each bucket!
1 | pthread_mutex_t lock[NBUCKET]; |
For concurrent put , we need to acquire lock for each bucket!
1 |
|
Barriers
We need all threads reach the barrier , we go the next stage. So we need to sleep a thread when it reaches. When all threads reach the barrier , clear the cnt 、 increase the round and wakeup other sleep thread!
1 | static void |