04 03 Threads and Concurrency Part 3 — Transcript
Full transcript
- 0:08[Music]
- 0:22all right so next would be the thread
- 0:24libraries
- 0:26so thread libraries provide programmers
- 0:29with an api for creating and managing
- 0:31threats
- 0:32okay
- 0:33so thread libraries may be implemented
- 0:36either in the user space or the kernel
- 0:39space
- 0:40so the former involves an
- 0:42api or api functions implemented solely
- 0:46within the user space with no kernel
- 0:48support all right
- 0:51so
- 0:52the latter involved system calls
- 0:54okay so and requires a kernel
- 0:57with tread library support
- 0:59so there are three main thread libraries
- 1:02in use today okay so these are the posix
- 1:05threads
- 1:06okay our p threads the win32 threads and
- 1:09the java threads
- 1:10all right
- 1:12now let's talk about the p trends okay
- 1:16so p threads may be provided either a
- 1:19user level or a kernel level
- 1:22okay so the posix standard is an ieee
- 1:251003 that 1c defines the specification
- 1:29for pthreads not the implementation
- 1:32okay so specification not an
- 1:34implementation
- 1:36so global variables are shared amongst
- 1:39all threads
- 1:40okay
- 1:41and one chart can wait for the others
- 1:45to rejoin before continuing that's p
- 1:47threads
- 1:54all right so next would be a java thread
- 1:57so java threads are managed by the java
- 1:59virtual machine okay so all java
- 2:02programs use a threads okay or this java
- 2:06threads
- 2:07even common single threaded ones so the
- 2:10creation of the new threads requires
- 2:13objects that
- 2:14implement
- 2:16the runnable interface which means
- 2:19they contain a method public void run so
- 2:23remember that in programming okay so any
- 2:26descendant of the thread class will
- 2:28naturally contain such method so in
- 2:30practice
- 2:31okay
- 2:32so
- 2:33the run method must be overridden
- 2:37provided for the thread to have any
- 2:40practical
- 2:41functionality
- 2:42okay so java trends may be created by
- 2:45extending the thread class implementing
- 2:47the runnable interfaces
- 2:49and standard practice is to implement a
- 2:51runnable interface
- 2:54so the next one would be implicit
- 2:55threading
- 2:57okay
- 2:59so implicit
- 3:01threading
- 3:03shifts the version of addressing the
- 3:05programming challenges
- 3:07outlined
- 3:08on this discussion okay
- 3:11and the programmer
- 3:14is using this implicit threading growing
- 3:17in popularity as the number of threads
- 3:19increase
- 3:20okay program correctness
- 3:22more difficult with explicit threats
- 3:25okay
- 3:26so creation and management of the
- 3:28threads done by compilers
- 3:32run time libraries rather than
- 3:34programmers so five methods are explored
- 3:36we've got the thread pools
- 3:38fork and join the openmp
- 3:41the grand central dispatch and the intel
- 3:43threading building blocks
- 3:46okay so let's start with thread pulls
- 3:49so in thread pulls creating a new thread
- 3:52every time one is needed
- 3:55and then deleting it when it is done can
- 3:58be inefficient
- 4:00and can also lead to a very large or
- 4:03unlimited number of threads being
- 4:05created
- 4:07okay
- 4:08so advantages well usually slightly
- 4:10faster to service a request with an
- 4:12existing thread done creating a new
- 4:14thread so allows the number of threads
- 4:17in applications
- 4:18to be bound to the size of the pool
- 4:22and separating the task to be performed
- 4:24from mechanics
- 4:26of creating tasks allow different uh
- 4:29strategies
- 4:30for running tasks okay so example tasks
- 4:33could be scheduled to run periodically
- 4:36okay
- 4:37so the win32 okay the win32 api provides
- 4:42thread pulls
- 4:44through the pull function
- 4:45function
- 4:46okay so java also provides support for
- 4:49thread pulls through the java that you
- 4:52till that concurrent package
- 4:54and apple supports thread pools under
- 4:56the grand central dispatch architecture
- 5:00okay
- 5:02so what are the threading issues
- 5:04so semantics
- 5:06of forks and exec system calls signal
- 5:09handling thread cancellation of target
- 5:12thread
- 5:12okay thread local storage and scheduler
- 5:16activations
- 5:17so let's discuss this
- 5:20uh threading issues on the next slide
- 5:23okay so let's start with the semantic of
- 5:25fork and exec okay
- 5:28so
- 5:30does fork duplicate only by calling
- 5:33tread
- 5:34or all threads
- 5:35well some unixes have two versions of
- 5:38word okay so exact usually works as
- 5:41normal replace the running process
- 5:44including all threads
- 5:46if one of the thread forks is the entire
- 5:49process copied or is the new process
- 5:52single threaded
- 5:54well that's system dependent
- 5:56okay
- 5:57what if the new process execs right away
- 6:01there is no need to copy all other
- 6:03threads
- 6:04if it doesn't then the entire process
- 6:07should be copied well again many
- 6:10versions of unix provides multiple
- 6:12versions of fork
- 6:14for
- 6:14uh for for the purpose or for that
- 6:17purpose okay so that's mentioned here
- 6:20okay
- 6:21now signal handling
- 6:25question
- 6:26so when a multi-threaded process
- 6:28receives a signal
- 6:30to what threads should that signal be
- 6:31delivered
- 6:34okay
- 6:35well
- 6:35there are four major options here okay
- 6:38so deliver the signal to the thread to
- 6:41which the signal applies
- 6:44okay
- 6:45next is deliver the signal to every
- 6:48thread in the process
- 6:50deliver the signal to certain threads in
- 6:52the process
- 6:54or assign a specific thread to receive
- 6:56all signals in the process
- 7:00okay so the best choice may depend on
- 7:03which specific signal is involved
- 7:06all right
- 7:08okay
- 7:10so unix allows individual threads to
- 7:12indicate which signals
- 7:14they are accepting and which they are
- 7:16ignoring so however
- 7:18the signal can only be delivered to one
- 7:20thread
- 7:22okay so which is generally the first
- 7:24thread that is accepting that particular
- 7:26signal
- 7:27so unix provides two separate system
- 7:30calls
- 7:31you've got the kill
- 7:32okay the pin signal and the featured
- 7:35underscore
- 7:36kill or the tilden signal so for
- 7:38delivering signals to process s or
- 7:41specific threads respectively
- 7:44so windows does not support signals
- 7:47but they can emulate using asynchronous
- 7:50procedure calls or the apc
- 7:54okay
- 7:55so a pieces are delivered to specific
- 7:58threads not on the process
- 8:02okay so another easy start cancellation
- 8:04how can we cancel a specific thread in
- 8:06the process
- 8:07okay so terminating a tread before it
- 8:10has finished or thread to be cancelled
- 8:13is the target thread
- 8:14so two general approaches are being
- 8:17observed okay we're gonna have the
- 8:19asynchronous cancellation
- 8:21which can sell threads immediately okay
- 8:24and you've got the deferred cancellation
- 8:27okay sets a flag indicating that thread
- 8:30should cancel itself when it is
- 8:32convenient
- 8:34so it is then
- 8:35up to the canceled thread to check this
- 8:37flag periodically
- 8:39and exit nicely
- 8:41when it sees the flag set
- 8:44all right
- 8:45so
- 8:46that's thread cancellation in there okay
- 8:49so the shared or resource allocation
- 8:52and the inter-threaded
- 8:54data transfers can be problematic with a
- 8:56synchronous cancellation
- 8:58all right
- 9:02so invoking a chart cancellation request
- 9:05cancellation but actual cancellation
- 9:07depends on the trend state
- 9:10okay so if thread has cancellation
- 9:12disabled cancellation remains pending
- 9:15until thread enables it
- 9:18okay so on linux systems thread
- 9:21cancellation is handled through signals
- 9:24all right
- 9:26next would be the thread local storage
- 9:29or the tls so the tls allows its thread
- 9:32to have its own copy of the data so this
- 9:35would be useful
- 9:36when you do not have the control over
- 9:38the thread creation process for example
- 9:40when you're using thread pool okay
- 9:43so different from local variables so
- 9:46local variables visible only during a
- 9:48single function invocation
- 9:50well the tls visible across function
- 9:52invocations
- 9:54okay so this is similar to static data
- 9:56so tls is a unique
- 9:58twitch thread
- 10:00okay so most data is shared among
- 10:02threads okay
- 10:04and this is one of the major benefits of
- 10:06using threads in the first place however
- 10:09sometimes threads need to be thread
- 10:11specific data also
- 10:13okay so most major thread libraries
- 10:16including p threads
- 10:18win32 or java provide support for
- 10:21trans-specific data known as
- 10:24this tls here
- 10:26okay
- 10:27so note that this is more like static
- 10:30that local variable or the local
- 10:33variable as mentioned
- 10:34okay because it does not cease to exist
- 10:37when the function ends that's a good
- 10:39thing about it
- 10:40all right
- 10:42okay
- 10:43so next would be the scheduler
- 10:46activations
- 10:47okay
- 10:48so many implementations of threads
- 10:51provide a virtual processor as an
- 10:53interface between the user thread
- 10:55and the kernel thread so particularly
- 10:58for the many too many or the two-tier
- 11:01models
- 11:02okay so the virtual processor is known
- 11:04as the life that process
- 11:07or lwp
- 11:09okay
- 11:10so there is one-to-one correspondence
- 11:13between the lwp
- 11:16and the kernel threads
- 11:18okay so the number of kernel threads
- 11:20available
- 11:21and hence the number of lwp's may change
- 11:24dynamically so the application user
- 11:29level thread library maps user threads
- 11:32onto an available lwp's
- 11:34okay
- 11:35so this communication allows an
- 11:37application to maintain the correct
- 11:39number of kernel threads
- 11:41right
- 11:43okay so next would be the operating
- 11:46system example so we'll be talking about
- 11:48the windows and linux threads here
- 11:50okay so for the windows threads
- 11:53so we are using the windows api or the
- 11:56win32 okay
- 11:58win32 api thread library that supports
- 12:01one to one thread model
- 12:03so win32
- 12:05also provides a fiverr
- 12:08uh library
- 12:10which supports the many-to-many model
- 12:13okay so the win32 thread component
- 12:15includes
- 12:17the thread id okay registers
- 12:21uh a user stack used in user mode and
- 12:26the kernel stock used in kernel mode
- 12:29okay
- 12:31and
- 12:32a private storage area used by various
- 12:34runtime libraries and dynamic link
- 12:36libraries
- 12:38okay
- 12:40so the primary data structures of thread
- 12:43includes the is thread k thread and
- 12:47this thread environment block here
- 12:49okay so the e thread is the executive
- 12:51thread block includes pointer to process
- 12:54to which thread belongs to
- 12:57and to k-thread internal space
- 13:00so as a k-thread internal thread block
- 13:03scheduling and synchronization
- 13:04information
- 13:06kernel block stop pointer to peb in a
- 13:09kernel space
- 13:11okay and the last one is the thread
- 13:13environment block
- 13:15thread id user mode stack thread local
- 13:18storage in the user space
- 13:20okay
- 13:21so each thread is in the kernel space
- 13:23k thread is the kernel space and teb is
- 13:26in the user space
- 13:28okay so let's talk about the linux
- 13:30styles now
- 13:31so linux refers to the or refers them as
- 13:35tasks rather than threads
- 13:38okay so in windows in other operating
- 13:40systems we call it trends in linux it's
- 13:42called tasks
- 13:44all right so linux does not distinguish
- 13:47between the process and trends
- 13:50okay
- 13:51so it uses the more generic term tasks
- 13:54here so when you have heard of the term
- 13:56task that pertains to linux threads
- 13:59okay
- 14:00so the traditional fork system call
- 14:04completely duplicates a process or task
- 14:07as described earlier okay
- 14:09so
- 14:11don't be confused with the term okay
- 14:14so an alternative system called clone
- 14:17okay
- 14:18which allows a child task to share the
- 14:20address space of the parent tasks
- 14:23or process
- 14:25okay
- 14:26so calling clone with no flags
- 14:28is set equivalent to fork
- 14:31all right
- 14:33and calling clone
- 14:35with clone fs
- 14:37all right you've got clone fs here clone
- 14:40vm
- 14:42clone
- 14:44sync hand
- 14:45and clone files
- 14:48these are all equivalent to creating a
- 14:50trend
- 14:51okay
- 14:52as all these data structures will be
- 14:55shared
- 14:57all right so the struck task struck
- 14:59points or process data structures shared
- 15:02or
- 15:03unique
- 15:05okay
- 15:07now linux implements this using the
- 15:09structure task strap
- 15:13which essentially provides a level of
- 15:15indirection
- 15:17to task resources so when the flags are
- 15:20not set
- 15:21then the resources pointed to the
- 15:23structure are copied
- 15:25but if the flags are set
- 15:27then only the pointers and the resources
- 15:29are copied and hence the resources are
- 15:32shared
- 15:33okay
- 15:36so think about the deep copy versus the
- 15:39swallow copy and object oriented
- 15:41programming
- 15:42okay so that's how it works
- 15:45okay supposex compliant
- 15:47support for smp or the symmetric
- 15:50multiprocessing
- 15:51you've got the numa or numa the
- 15:54non-uniform memory access
- 15:56and a multi-core processors
- 15:59also supports for hundreds of thousands
- 16:02of trends
- 16:13[Music]
- 16:21you
About this transcript
This page contains the full transcript of 04 03 Threads and Concurrency Part 3 by Santelmo, generated from the public captions YouTube serves with the video. The transcript has 1,885 words across 413 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.