Skip to content

Commit 696f909

Browse files
committed
Updated animation
1 parent b5baf4a commit 696f909

File tree

3 files changed

+75
-77
lines changed

3 files changed

+75
-77
lines changed

animation/parallelism/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

animation/parallelism/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"devDependencies": {
1818
"@motion-canvas/ui": "^3.16.0",
1919
"@motion-canvas/vite-plugin": "^3.15.1",
20-
"typescript": "^5.2.2",
20+
"typescript": "^5.9.3",
2121
"vite": "^4.0.0"
2222
}
2323
}

animation/parallelism/src/scenes/code.tsx

Lines changed: 73 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -325,40 +325,10 @@ public:
325325
std::cout << "ImageProcessor is shutting down.\\n";
326326
}
327327
328-
private:
329-
void ProcessImages(std::stop_token stoken, int worker_id);
330-
331-
std::queue<TinyImage> image_queue_;
332-
std::mutex cout_mutex_;
333-
std::mutex queue_mutex_;
334-
std::condition_variable_any cv_; // C++20 feature: supports stop_token seamlessly
335-
std::vector<std::jthread> workers_;
336-
};`;
337-
338-
yield* all(
339-
codeRef().code(jthread_2, duration),
340-
codeRef().fontSize(15, duration),
341-
codeRef().y(-100, duration),
342-
);
343-
yield* waitFor(duration);
344-
345-
// Focus constructor and submit
346-
yield* all(
347-
codeRef().selection(lines(27, 45), duration),
348-
codeRef().fontSize(22, duration),
349-
codeRef().y(200, duration),
350-
);
351-
yield* waitFor(duration);
352-
353-
const jthread_3 = jthread_1 + `\n
354-
class ImageProcessor {
355-
// ...
356328
private:
357329
void ProcessImages(std::stop_token stoken, int worker_id) {
358-
359330
while (true) {
360331
TinyImage img;
361-
362332
{
363333
std::unique_lock<std::mutex> lock(queue_mutex_);
364334
// Wait until there's work to do, or we are told to stop.
@@ -380,7 +350,6 @@ private:
380350
} // The lock is automatically released here!
381351
382352
img.Process();
383-
384353
{
385354
std::lock_guard<std::mutex> cout_lock(cout_mutex_);
386355
std::cout << "Worker " << worker_id << " processed image " << img.id << "!\\n";
@@ -395,24 +364,38 @@ private:
395364
std::vector<std::jthread> workers_;
396365
};`;
397366

367+
yield* all(
368+
codeRef().code(jthread_2, duration),
369+
codeRef().fontSize(15, duration),
370+
codeRef().y(-100, duration),
371+
);
372+
yield* waitFor(duration);
373+
374+
// Focus constructor and submit
375+
yield* all(
376+
codeRef().selection(lines(24, 43), duration),
377+
codeRef().fontSize(22, duration),
378+
codeRef().y(400, duration), // Move up to see top of block
379+
);
380+
yield* waitFor(duration);
381+
398382
yield* all(
399383
codeRef().selection(DEFAULT, duration),
400-
codeRef().fontSize(18, duration),
401-
codeRef().y(-300, duration),
402-
codeRef().code(jthread_3, duration),
384+
codeRef().fontSize(15, duration),
385+
codeRef().y(-100, duration),
403386
);
404387
yield* waitFor(duration);
405388

406-
// Focus on the locking and cv
389+
// Focus on the locking and cv in ProcessImages
407390
yield* all(
408-
codeRef().selection(lines(33, 51), duration),
391+
codeRef().selection(lines(58, 67), duration),
409392
codeRef().fontSize(22, duration),
410-
codeRef().y(500, duration),
393+
codeRef().y(-1000, duration), // Move down to see bottom part
411394
);
412395
yield* waitFor(duration);
413396

414397
// Wait with main
415-
const jthread_4 = jthread_3 + `\n
398+
const jthread_3 = jthread_2 + `\n
416399
int main() {
417400
ImageProcessor processor(4);
418401
@@ -429,28 +412,28 @@ int main() {
429412

430413
yield* all(
431414
codeRef().selection(DEFAULT, duration),
432-
codeRef().fontSize(18, duration),
433-
codeRef().y(-400, duration),
434-
codeRef().code(jthread_4, duration),
415+
codeRef().fontSize(15, duration),
416+
codeRef().y(-500, duration),
417+
codeRef().code(jthread_3, duration),
435418
);
436419
yield* waitFor(duration);
437420

438421
yield* all(
439-
codeRef().selection(lines(68, 80), duration),
422+
codeRef().selection(lines(86, 97), duration),
440423
codeRef().fontSize(22, duration),
441-
codeRef().y(450, duration),
424+
codeRef().y(-1600, duration), // Scroll all the way down to main
442425
);
443426
yield* waitFor(duration);
444427

445-
// 5. Thread Pool C++17 Equivalent
428+
// 5. Thread Pool C++17 Equivalent (Morphing from C++20)
446429
yield* all(
447430
codeRef().selection(DEFAULT, duration),
448-
codeRef().fontSize(20, duration),
449-
codeRef().y(0, duration),
450-
codeRef().code('', duration),
431+
codeRef().fontSize(15, duration),
432+
codeRef().y(-400, duration),
451433
);
434+
yield* waitFor(duration);
452435

453-
const cpp17_1 = `\
436+
const cpp17_1 = jthread_1 + `\n
454437
class ImageProcessor {
455438
public:
456439
ImageProcessor(int num_workers) {
@@ -462,6 +445,14 @@ public:
462445
}
463446
}
464447
448+
void Submit(TinyImage img) {
449+
{
450+
std::lock_guard<std::mutex> lock(queue_mutex_);
451+
image_queue_.push(std::move(img));
452+
}
453+
cv_.notify_one();
454+
}
455+
465456
void Shutdown() {
466457
{
467458
std::lock_guard<std::mutex> cout_lock(cout_mutex_);
@@ -487,28 +478,11 @@ public:
487478
~ImageProcessor() {
488479
Shutdown(); // Ensure cleanup happens even if user forgets to call Shutdown
489480
}
490-
// ...
491-
};`;
492-
493-
yield* codeRef().code(cpp17_1, duration);
494-
yield* waitFor(duration);
495481
496-
// Focus on shutdown logic
497-
yield* all(
498-
codeRef().selection(lines(11, 31), duration),
499-
codeRef().fontSize(22, duration),
500-
codeRef().y(150, duration),
501-
);
502-
yield* waitFor(duration);
503-
504-
const cpp17_2 = `\
505-
class ImageProcessor {
506-
// ...
507482
private:
508483
void ProcessImages(int worker_id) {
509484
while (true) {
510485
TinyImage img;
511-
512486
{
513487
std::unique_lock<std::mutex> lock(queue_mutex_);
514488
// Wait until there's work to do, or we are told to stop.
@@ -529,6 +503,10 @@ private:
529503
}
530504
531505
img.Process();
506+
{
507+
std::lock_guard<std::mutex> cout_lock(cout_mutex_);
508+
std::cout << "Worker " << worker_id << " processed image " << img.id << "!\\n";
509+
}
532510
}
533511
}
534512
@@ -538,38 +516,58 @@ private:
538516
std::condition_variable cv_;
539517
bool done_producing_ = false;
540518
std::vector<std::thread> workers_;
541-
};`;
519+
};\n
520+
int main() {
521+
ImageProcessor processor(4);
522+
523+
for (int i = 1; i <= 15; ++i) {
524+
processor.Submit(TinyImage{i});
525+
}
526+
527+
return 0;
528+
// Destructor fires here, joining everything!
529+
}`;
530+
531+
// Morph the C++20 thread pool cleanly into the C++17 one!
532+
yield* codeRef().code(cpp17_1, duration);
533+
yield* waitFor(duration);
534+
535+
// Focus on shutdown logic
536+
yield* all(
537+
codeRef().selection(lines(43, 63), duration),
538+
codeRef().fontSize(22, duration),
539+
codeRef().y(0, duration),
540+
);
541+
yield* waitFor(duration);
542542

543543
yield* all(
544544
codeRef().selection(DEFAULT, duration),
545-
codeRef().fontSize(18, duration),
546-
codeRef().y(-150, duration),
547-
codeRef().code(cpp17_2, duration),
545+
codeRef().fontSize(15, duration),
546+
codeRef().y(-400, duration),
548547
);
549548
yield* waitFor(duration);
550549

551550
// Focus CV condition
552551
yield* all(
553-
codeRef().selection(lines(9, 12), duration),
552+
codeRef().selection(lines(74, 77), duration),
554553
codeRef().fontSize(25, duration),
555-
codeRef().y(300, duration),
554+
codeRef().y(-600, duration),
556555
);
557556
yield* waitFor(duration);
558557

559558
// Focus on the data members changing
560559
yield* all(
561-
codeRef().selection(lines(30, 35), duration),
560+
codeRef().selection(lines(97, 103), duration),
562561
codeRef().fontSize(22, duration),
563-
codeRef().y(-50, duration),
562+
codeRef().y(-1300, duration),
564563
);
565564
yield* waitFor(duration);
566565

567566
// Finally wrap it out
568567
yield* all(
569568
codeRef().selection(DEFAULT, duration),
570569
codeRef().fontSize(20, duration),
571-
codeRef().y(0, duration),
572-
codeRef().code('', duration),
570+
codeRef().y(-300, duration),
573571
);
574572
yield* waitFor(duration);
575573

0 commit comments

Comments
 (0)