YouTube2Text

04 03 Threads and Concurrency Part 3 — Transcript

by Santelmo · 1,885 words · 413 segments · language en · Watch on YouTube

Full transcript

  1. 0:08[Music]
  2. 0:22all right so next would be the thread
  3. 0:24libraries
  4. 0:26so thread libraries provide programmers
  5. 0:29with an api for creating and managing
  6. 0:31threats
  7. 0:32okay
  8. 0:33so thread libraries may be implemented
  9. 0:36either in the user space or the kernel
  10. 0:39space
  11. 0:40so the former involves an
  12. 0:42api or api functions implemented solely
  13. 0:46within the user space with no kernel
  14. 0:48support all right
  15. 0:51so
  16. 0:52the latter involved system calls
  17. 0:54okay so and requires a kernel
  18. 0:57with tread library support
  19. 0:59so there are three main thread libraries
  20. 1:02in use today okay so these are the posix
  21. 1:05threads
  22. 1:06okay our p threads the win32 threads and
  23. 1:09the java threads
  24. 1:10all right
  25. 1:12now let's talk about the p trends okay
  26. 1:16so p threads may be provided either a
  27. 1:19user level or a kernel level
  28. 1:22okay so the posix standard is an ieee
  29. 1:251003 that 1c defines the specification
  30. 1:29for pthreads not the implementation
  31. 1:32okay so specification not an
  32. 1:34implementation
  33. 1:36so global variables are shared amongst
  34. 1:39all threads
  35. 1:40okay
  36. 1:41and one chart can wait for the others
  37. 1:45to rejoin before continuing that's p
  38. 1:47threads
  39. 1:54all right so next would be a java thread
  40. 1:57so java threads are managed by the java
  41. 1:59virtual machine okay so all java
  42. 2:02programs use a threads okay or this java
  43. 2:06threads
  44. 2:07even common single threaded ones so the
  45. 2:10creation of the new threads requires
  46. 2:13objects that
  47. 2:14implement
  48. 2:16the runnable interface which means
  49. 2:19they contain a method public void run so
  50. 2:23remember that in programming okay so any
  51. 2:26descendant of the thread class will
  52. 2:28naturally contain such method so in
  53. 2:30practice
  54. 2:31okay
  55. 2:32so
  56. 2:33the run method must be overridden
  57. 2:37provided for the thread to have any
  58. 2:40practical
  59. 2:41functionality
  60. 2:42okay so java trends may be created by
  61. 2:45extending the thread class implementing
  62. 2:47the runnable interfaces
  63. 2:49and standard practice is to implement a
  64. 2:51runnable interface
  65. 2:54so the next one would be implicit
  66. 2:55threading
  67. 2:57okay
  68. 2:59so implicit
  69. 3:01threading
  70. 3:03shifts the version of addressing the
  71. 3:05programming challenges
  72. 3:07outlined
  73. 3:08on this discussion okay
  74. 3:11and the programmer
  75. 3:14is using this implicit threading growing
  76. 3:17in popularity as the number of threads
  77. 3:19increase
  78. 3:20okay program correctness
  79. 3:22more difficult with explicit threats
  80. 3:25okay
  81. 3:26so creation and management of the
  82. 3:28threads done by compilers
  83. 3:32run time libraries rather than
  84. 3:34programmers so five methods are explored
  85. 3:36we've got the thread pools
  86. 3:38fork and join the openmp
  87. 3:41the grand central dispatch and the intel
  88. 3:43threading building blocks
  89. 3:46okay so let's start with thread pulls
  90. 3:49so in thread pulls creating a new thread
  91. 3:52every time one is needed
  92. 3:55and then deleting it when it is done can
  93. 3:58be inefficient
  94. 4:00and can also lead to a very large or
  95. 4:03unlimited number of threads being
  96. 4:05created
  97. 4:07okay
  98. 4:08so advantages well usually slightly
  99. 4:10faster to service a request with an
  100. 4:12existing thread done creating a new
  101. 4:14thread so allows the number of threads
  102. 4:17in applications
  103. 4:18to be bound to the size of the pool
  104. 4:22and separating the task to be performed
  105. 4:24from mechanics
  106. 4:26of creating tasks allow different uh
  107. 4:29strategies
  108. 4:30for running tasks okay so example tasks
  109. 4:33could be scheduled to run periodically
  110. 4:36okay
  111. 4:37so the win32 okay the win32 api provides
  112. 4:42thread pulls
  113. 4:44through the pull function
  114. 4:45function
  115. 4:46okay so java also provides support for
  116. 4:49thread pulls through the java that you
  117. 4:52till that concurrent package
  118. 4:54and apple supports thread pools under
  119. 4:56the grand central dispatch architecture
  120. 5:00okay
  121. 5:02so what are the threading issues
  122. 5:04so semantics
  123. 5:06of forks and exec system calls signal
  124. 5:09handling thread cancellation of target
  125. 5:12thread
  126. 5:12okay thread local storage and scheduler
  127. 5:16activations
  128. 5:17so let's discuss this
  129. 5:20uh threading issues on the next slide
  130. 5:23okay so let's start with the semantic of
  131. 5:25fork and exec okay
  132. 5:28so
  133. 5:30does fork duplicate only by calling
  134. 5:33tread
  135. 5:34or all threads
  136. 5:35well some unixes have two versions of
  137. 5:38word okay so exact usually works as
  138. 5:41normal replace the running process
  139. 5:44including all threads
  140. 5:46if one of the thread forks is the entire
  141. 5:49process copied or is the new process
  142. 5:52single threaded
  143. 5:54well that's system dependent
  144. 5:56okay
  145. 5:57what if the new process execs right away
  146. 6:01there is no need to copy all other
  147. 6:03threads
  148. 6:04if it doesn't then the entire process
  149. 6:07should be copied well again many
  150. 6:10versions of unix provides multiple
  151. 6:12versions of fork
  152. 6:14for
  153. 6:14uh for for the purpose or for that
  154. 6:17purpose okay so that's mentioned here
  155. 6:20okay
  156. 6:21now signal handling
  157. 6:25question
  158. 6:26so when a multi-threaded process
  159. 6:28receives a signal
  160. 6:30to what threads should that signal be
  161. 6:31delivered
  162. 6:34okay
  163. 6:35well
  164. 6:35there are four major options here okay
  165. 6:38so deliver the signal to the thread to
  166. 6:41which the signal applies
  167. 6:44okay
  168. 6:45next is deliver the signal to every
  169. 6:48thread in the process
  170. 6:50deliver the signal to certain threads in
  171. 6:52the process
  172. 6:54or assign a specific thread to receive
  173. 6:56all signals in the process
  174. 7:00okay so the best choice may depend on
  175. 7:03which specific signal is involved
  176. 7:06all right
  177. 7:08okay
  178. 7:10so unix allows individual threads to
  179. 7:12indicate which signals
  180. 7:14they are accepting and which they are
  181. 7:16ignoring so however
  182. 7:18the signal can only be delivered to one
  183. 7:20thread
  184. 7:22okay so which is generally the first
  185. 7:24thread that is accepting that particular
  186. 7:26signal
  187. 7:27so unix provides two separate system
  188. 7:30calls
  189. 7:31you've got the kill
  190. 7:32okay the pin signal and the featured
  191. 7:35underscore
  192. 7:36kill or the tilden signal so for
  193. 7:38delivering signals to process s or
  194. 7:41specific threads respectively
  195. 7:44so windows does not support signals
  196. 7:47but they can emulate using asynchronous
  197. 7:50procedure calls or the apc
  198. 7:54okay
  199. 7:55so a pieces are delivered to specific
  200. 7:58threads not on the process
  201. 8:02okay so another easy start cancellation
  202. 8:04how can we cancel a specific thread in
  203. 8:06the process
  204. 8:07okay so terminating a tread before it
  205. 8:10has finished or thread to be cancelled
  206. 8:13is the target thread
  207. 8:14so two general approaches are being
  208. 8:17observed okay we're gonna have the
  209. 8:19asynchronous cancellation
  210. 8:21which can sell threads immediately okay
  211. 8:24and you've got the deferred cancellation
  212. 8:27okay sets a flag indicating that thread
  213. 8:30should cancel itself when it is
  214. 8:32convenient
  215. 8:34so it is then
  216. 8:35up to the canceled thread to check this
  217. 8:37flag periodically
  218. 8:39and exit nicely
  219. 8:41when it sees the flag set
  220. 8:44all right
  221. 8:45so
  222. 8:46that's thread cancellation in there okay
  223. 8:49so the shared or resource allocation
  224. 8:52and the inter-threaded
  225. 8:54data transfers can be problematic with a
  226. 8:56synchronous cancellation
  227. 8:58all right
  228. 9:02so invoking a chart cancellation request
  229. 9:05cancellation but actual cancellation
  230. 9:07depends on the trend state
  231. 9:10okay so if thread has cancellation
  232. 9:12disabled cancellation remains pending
  233. 9:15until thread enables it
  234. 9:18okay so on linux systems thread
  235. 9:21cancellation is handled through signals
  236. 9:24all right
  237. 9:26next would be the thread local storage
  238. 9:29or the tls so the tls allows its thread
  239. 9:32to have its own copy of the data so this
  240. 9:35would be useful
  241. 9:36when you do not have the control over
  242. 9:38the thread creation process for example
  243. 9:40when you're using thread pool okay
  244. 9:43so different from local variables so
  245. 9:46local variables visible only during a
  246. 9:48single function invocation
  247. 9:50well the tls visible across function
  248. 9:52invocations
  249. 9:54okay so this is similar to static data
  250. 9:56so tls is a unique
  251. 9:58twitch thread
  252. 10:00okay so most data is shared among
  253. 10:02threads okay
  254. 10:04and this is one of the major benefits of
  255. 10:06using threads in the first place however
  256. 10:09sometimes threads need to be thread
  257. 10:11specific data also
  258. 10:13okay so most major thread libraries
  259. 10:16including p threads
  260. 10:18win32 or java provide support for
  261. 10:21trans-specific data known as
  262. 10:24this tls here
  263. 10:26okay
  264. 10:27so note that this is more like static
  265. 10:30that local variable or the local
  266. 10:33variable as mentioned
  267. 10:34okay because it does not cease to exist
  268. 10:37when the function ends that's a good
  269. 10:39thing about it
  270. 10:40all right
  271. 10:42okay
  272. 10:43so next would be the scheduler
  273. 10:46activations
  274. 10:47okay
  275. 10:48so many implementations of threads
  276. 10:51provide a virtual processor as an
  277. 10:53interface between the user thread
  278. 10:55and the kernel thread so particularly
  279. 10:58for the many too many or the two-tier
  280. 11:01models
  281. 11:02okay so the virtual processor is known
  282. 11:04as the life that process
  283. 11:07or lwp
  284. 11:09okay
  285. 11:10so there is one-to-one correspondence
  286. 11:13between the lwp
  287. 11:16and the kernel threads
  288. 11:18okay so the number of kernel threads
  289. 11:20available
  290. 11:21and hence the number of lwp's may change
  291. 11:24dynamically so the application user
  292. 11:29level thread library maps user threads
  293. 11:32onto an available lwp's
  294. 11:34okay
  295. 11:35so this communication allows an
  296. 11:37application to maintain the correct
  297. 11:39number of kernel threads
  298. 11:41right
  299. 11:43okay so next would be the operating
  300. 11:46system example so we'll be talking about
  301. 11:48the windows and linux threads here
  302. 11:50okay so for the windows threads
  303. 11:53so we are using the windows api or the
  304. 11:56win32 okay
  305. 11:58win32 api thread library that supports
  306. 12:01one to one thread model
  307. 12:03so win32
  308. 12:05also provides a fiverr
  309. 12:08uh library
  310. 12:10which supports the many-to-many model
  311. 12:13okay so the win32 thread component
  312. 12:15includes
  313. 12:17the thread id okay registers
  314. 12:21uh a user stack used in user mode and
  315. 12:26the kernel stock used in kernel mode
  316. 12:29okay
  317. 12:31and
  318. 12:32a private storage area used by various
  319. 12:34runtime libraries and dynamic link
  320. 12:36libraries
  321. 12:38okay
  322. 12:40so the primary data structures of thread
  323. 12:43includes the is thread k thread and
  324. 12:47this thread environment block here
  325. 12:49okay so the e thread is the executive
  326. 12:51thread block includes pointer to process
  327. 12:54to which thread belongs to
  328. 12:57and to k-thread internal space
  329. 13:00so as a k-thread internal thread block
  330. 13:03scheduling and synchronization
  331. 13:04information
  332. 13:06kernel block stop pointer to peb in a
  333. 13:09kernel space
  334. 13:11okay and the last one is the thread
  335. 13:13environment block
  336. 13:15thread id user mode stack thread local
  337. 13:18storage in the user space
  338. 13:20okay
  339. 13:21so each thread is in the kernel space
  340. 13:23k thread is the kernel space and teb is
  341. 13:26in the user space
  342. 13:28okay so let's talk about the linux
  343. 13:30styles now
  344. 13:31so linux refers to the or refers them as
  345. 13:35tasks rather than threads
  346. 13:38okay so in windows in other operating
  347. 13:40systems we call it trends in linux it's
  348. 13:42called tasks
  349. 13:44all right so linux does not distinguish
  350. 13:47between the process and trends
  351. 13:50okay
  352. 13:51so it uses the more generic term tasks
  353. 13:54here so when you have heard of the term
  354. 13:56task that pertains to linux threads
  355. 13:59okay
  356. 14:00so the traditional fork system call
  357. 14:04completely duplicates a process or task
  358. 14:07as described earlier okay
  359. 14:09so
  360. 14:11don't be confused with the term okay
  361. 14:14so an alternative system called clone
  362. 14:17okay
  363. 14:18which allows a child task to share the
  364. 14:20address space of the parent tasks
  365. 14:23or process
  366. 14:25okay
  367. 14:26so calling clone with no flags
  368. 14:28is set equivalent to fork
  369. 14:31all right
  370. 14:33and calling clone
  371. 14:35with clone fs
  372. 14:37all right you've got clone fs here clone
  373. 14:40vm
  374. 14:42clone
  375. 14:44sync hand
  376. 14:45and clone files
  377. 14:48these are all equivalent to creating a
  378. 14:50trend
  379. 14:51okay
  380. 14:52as all these data structures will be
  381. 14:55shared
  382. 14:57all right so the struck task struck
  383. 14:59points or process data structures shared
  384. 15:02or
  385. 15:03unique
  386. 15:05okay
  387. 15:07now linux implements this using the
  388. 15:09structure task strap
  389. 15:13which essentially provides a level of
  390. 15:15indirection
  391. 15:17to task resources so when the flags are
  392. 15:20not set
  393. 15:21then the resources pointed to the
  394. 15:23structure are copied
  395. 15:25but if the flags are set
  396. 15:27then only the pointers and the resources
  397. 15:29are copied and hence the resources are
  398. 15:32shared
  399. 15:33okay
  400. 15:36so think about the deep copy versus the
  401. 15:39swallow copy and object oriented
  402. 15:41programming
  403. 15:42okay so that's how it works
  404. 15:45okay supposex compliant
  405. 15:47support for smp or the symmetric
  406. 15:50multiprocessing
  407. 15:51you've got the numa or numa the
  408. 15:54non-uniform memory access
  409. 15:56and a multi-core processors
  410. 15:59also supports for hundreds of thousands
  411. 16:02of trends
  412. 16:13[Music]
  413. 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.