Async Engine in C

แชร์
ฝัง
  • เผยแพร่เมื่อ 10 พ.ค. 2024
  • Streamed Live on Twitch: / tsoding
    Enable CC for Twitch Chat
    Panim Playlist: • Panim
    References:
    - caseymuratori.com/blog_0015
    Support:
    - BTC: bc1qj820dmeazpeq5pjn89mlh9lhws7ghs9v34x9v9
    - Pay for my VPS: zap-hosting.com/en/shop/donat...
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 106

  • @marcs9451
    @marcs9451 17 วันที่ผ่านมา +184

    I'm async, I like the video before it is finished loading

    • @TieMajor
      @TieMajor 17 วันที่ผ่านมา +6

      I liked the comment before I finished reading it

    • @ru2979
      @ru2979 17 วันที่ผ่านมา

      😮

    • @IamRishavDas
      @IamRishavDas 15 วันที่ผ่านมา +1

      ​@@TieMajorI know your message before reading it.

  • @artemiasalina1860
    @artemiasalina1860 17 วันที่ผ่านมา +63

    "Wait, my emacs is open, I just confused it with a terminal"
    Can your VsCode do that? Can your VsCode do that?

    • @semihkaplan
      @semihkaplan 17 วันที่ผ่านมา +4

      Ofc it can

  • @boccobadz
    @boccobadz 17 วันที่ผ่านมา +11

    C was my favourite language some 15 years ago but I slowly pivoted to Python, and then to Go. Those streams are great showcase for what you can achieve by understanding core CS concepts (instead of being "react dev") even when using simple (simple doesn't mean easy) tools. Thank you for inspiring me to go back to low level stuff for my sideprojects and to try out Raylib. But I'm doing it via Zig.

  • @user-ji6ip7ou8d
    @user-ji6ip7ou8d 17 วันที่ผ่านมา +31

    One of the most epic titles ever ngl

  • @OREYG
    @OREYG 17 วันที่ผ่านมา +4

    About coroutines - the best explanation I can give is that its a tool to express second-order control flow. Imagine you have an interpreter of some language written in C, and it can do 'latent' action, actions spanning multiple frames (hey, that's exactly what you have). So, if you would put a breakpoint in your C code, you wouldn't see a control flow of your animation (it would step into the code that does queing), because its scattered around multiple functions of your Async Engine. But with coroutines, you can express them directly, in pseudocode: "item = acquire_item("background"); result_of_a1 = await action1(item); await action2(result_of_a1, item)" (its all non-blocking code), now, when stepping with a debugger, it would properly follow second-order control flow, skipping all the scheduling and bookkeeping parts, which would simplify your debugging experience (also note how action2 is parametrized with the result of action1, without coroutines this would involve you creating a struct that you pass around and otherwise would've been non-trivial, especially when you want to reuse this action).

  • @10e999
    @10e999 17 วันที่ผ่านมา +60

    • @igz5553
      @igz5553 17 วันที่ผ่านมา

      Why the placeholder

    • @valshaped
      @valshaped 17 วันที่ผ่านมา +1

      ​​@@igz5553asynchronous commenting with promises

    • @nazarbekalda8056
      @nazarbekalda8056 17 วันที่ผ่านมา +21

      Promise rejected with this one 🗣️🔥🔥

    • @Cmanorange
      @Cmanorange 17 วันที่ผ่านมา +1

      @@igz5553 async promise

    • @dealloc
      @dealloc 17 วันที่ผ่านมา +7

      Uncaught (in promise) ERR_INTERNET_DISCONNECTED

  • @wiseskeshom4673
    @wiseskeshom4673 15 วันที่ผ่านมา +1

    Thank you very much, this is a really useful stuff for a C learner.

  • @MarcelRobitaille
    @MarcelRobitaille 17 วันที่ผ่านมา +11

    The tea is a real problem. I moved to a new country and I can't get the type of tea I like at all. Not even something close to it. Last time I visited home I packed my checked bag full of tea.

    • @SP-st6tv
      @SP-st6tv 17 วันที่ผ่านมา

      I bet you like red tea. I had figured out that ordinary tea that I liked is called "red tea" around the world but it has black color.

  • @Cmanorange
    @Cmanorange 17 วันที่ผ่านมา +11

    we're hogging all the cpu cycles with this one 🗣🔥🔥

  • @DimaLifeIsGood
    @DimaLifeIsGood 17 วันที่ผ่านมา +5

    "single responsibility my ass.. f#ck all of that" :D

    • @tomwilliam7299
      @tomwilliam7299 17 วันที่ผ่านมา

      Fuck Isreal too 🤣🤣🤣🤣😁

  • @androth1502
    @androth1502 17 วันที่ผ่านมา +3

    Tsoding plugging the MS Windows logo is a special moment.

  • @JekasObps
    @JekasObps 17 วันที่ผ่านมา +2

    coroutines are rather interesting thing, they like functions that preserve state between calls. This allows you to suspend routine and return to it later making sort of cooperative multitasking between different pieces of logic.
    Its doing nothing with asyncronious, but you can implement coroutine that checks whenever timer is off to enter next stage or something...

    • @anon_y_mousse
      @anon_y_mousse 5 วันที่ผ่านมา

      The function strtok() in the standard library can be thought of as a coroutine.

  • @NyanCoder
    @NyanCoder 17 วันที่ผ่านมา +11

    5:41 so, there are four kinds of async I would highlight: processes, threads, coroutines and goroutines. AFAIK here's the difference:
    - *Processes* (OS-managed) different address space, no shared memory
    - *Threads* (OS-managed) in the same address space as parent process, memory is shared
    - *Coroutines* are more like taking turns in a single thread (no real parallelism, but I think it depends on implementation), memory is shared
    - *Goroutines* are, I think, the combination of threads and coroutines in Golang, there is a manager what controls subroutines and arrange them between threads, memory is shared
    I may be wrong about something, if so, please correct me in the replies

    • @eldonad
      @eldonad 17 วันที่ผ่านมา

      You are correct. Coroutines are sort of an abstraction over an event loop, where you run on a single thread, and functions can yield in turn when they wait for I/O operations or other coroutines to complete. They are run by a built-in scheduler, like the os scheduler for threads, but there is no preemptivity (i.e the scheduler cannot forcefully stop a function).
      It is important to note that you can combine coroutines and threads, for exemple by running coroutines in a thread pool, if you need that sort of thing. In that case you can have subordinate event loops that can send events to the main event loop when tasks are finished.

    • @valshaped
      @valshaped 17 วันที่ผ่านมา +12

      I think the non-Go-branded term for goroutine is "Green threading"

    • @aredrih6723
      @aredrih6723 17 วันที่ผ่านมา +5

      I think a more important axis is cooperative vs preemptive scheduling:
      - cooperative: you stop running by manually yielding (you or the language but you won't get stopped if you don't)
      - preemptive: you get (forcefully) paused after some time by the OS. It will later resume your program. This happens many times per second.
      You don't have an unlimited amount of cores so eventually, 2 different processes will take turns sharing a core/thread.
      IMHO, the distinction between process and thread is good to know if you have to pick one but you can always ask the OS to share some memory with another process if you want to.
      A coroutine is just the fancy name of a function that can pause itself (can produce a payload) and can be resumed (optionally with a payload).
      It is good to have if you're doing cooperative scheduling but you can do the same with callback.
      AFAIK, goroutine are a backlog of coroutine to run spread across executors (i.e. thread) that run them sequentially.
      They yield when doing channel and OS i/o (roughly). So enough infinite loop will hang a go application.
      If you want to mess around with a naked coroutine, Python and JavaScript's generator fit the description:
      - yield keyword for explicit... well yielding
      - optional payload both ways (method .send in Python and .next in js)).

    • @dealloc
      @dealloc 17 วันที่ผ่านมา +1

      @@valshaped And Green processes would be a combination of coroutines and processes. E.g. what is used in the Erlang runtime

    • @tripplefives1402
      @tripplefives1402 17 วันที่ผ่านมา

      From a hardware point of view:
      processes are separate virtual memory page tables, the timer interrupt handler routine invoked by the CPU and provided by the OS will determine which page table pointer to assign based on the scheduler.
      threads are separate stack pointers. that same timer interrupt handler routine will then change the stack pointer to the one belonging to the thread in the process that the scheduler thinks should run and calls iret instruction which pops the instruction pointer from the stack and runs from there.
      The other stuff is software abstractions.

  • @user-of6qt6nh5b
    @user-of6qt6nh5b 17 วันที่ผ่านมา +2

    Hi, can you tell about your Linux setup?

  • @Czeckie
    @Czeckie 15 วันที่ผ่านมา +1

    would adding rewind be possible? Seems to me very useful for interactive animation creation to go few frames back and hot reload. But mostly, I would be interested in how to implement such a feature. Maybe a ring buffer of states? Storing only diffs? But making it work with the rest of the system I have no idea

    • @anon_y_mousse
      @anon_y_mousse 5 วันที่ผ่านมา

      Use an array or some type of linked structure to store the tasks sequentially and you can just iterate backwards.

  • @RandomGeometryDashStuff
    @RandomGeometryDashStuff 15 วันที่ผ่านมา

    01:04:32 example: unlike tcl, scratch has types but there is no easy way to get type of value:
    [ switch to costume ( "0" / "0" ) ] behaves differently than [ switch to costume ( join "N" "aN" ) ]
    also scratch is harder than many other programming languages because there is no local variables and no case-sensitive string compare

  • @StevenMartinGuitar
    @StevenMartinGuitar 15 วันที่ผ่านมา +1

    1:13:27 Yeah you certainly don't want the nob to grow indefinitely. Maybe it's better to have a more compact, lightweight nob?

  • @aredrih6723
    @aredrih6723 17 วันที่ผ่านมา

    I think this is closer to a bare bone finate state machine (w/ entry and exit callback + some ticking callback) than async await.
    I know that FSM are a way to implement async function but the way entry and exit callback are used does turn it into its own thing.
    Also not a criticism, as Tsoding said, you first do the simplest possible solution and refactor; plus, the final implemtation won't be an of the shelf one (but contrasting it with one can be interesting).

    • @monad_tcp
      @monad_tcp 12 วันที่ผ่านมา +1

      but that's basically what async/await is, an FSM, the rest is just fattening syntactic sugar.

  • @dodgeclub7162
    @dodgeclub7162 17 วันที่ผ่านมา

    Who and how put the stream comments in subtitles

  • @Humble_Electronic_Musician
    @Humble_Electronic_Musician 7 วันที่ผ่านมา

    Awesome!

  • @hubstrangers3450
    @hubstrangers3450 12 วันที่ผ่านมา

    Thank you....

  • @BolasDear
    @BolasDear 17 วันที่ผ่านมา +9

    i dont understand what you do.

    • @theevilcottonball
      @theevilcottonball 17 วันที่ผ่านมา +3

      He edits text files. And then relies on external tools to eventually transform them into executable instructions. It is called prgramming, it is a real thing, look it up!

    • @BolasDear
      @BolasDear 17 วันที่ผ่านมา

      @@theevilcottonball I don't care, it is much boring.

    • @angelcaru
      @angelcaru 17 วันที่ผ่านมา +8

      @@BolasDear ???? how did you end up here?

    • @BolasDear
      @BolasDear 17 วันที่ผ่านมา

      @@angelcaru i thought he was making marshmellows recipes thas why i am here , actually i was roaming on american invention for some fun, i got this channel . most boring man on earth , he still had not answered my query where to get programming socks . in my city no body sells theose .see he makes jokes still i took them seriously.

    • @BolasDear
      @BolasDear 17 วันที่ผ่านมา

      @@theevilcottonball have you heard anyone who says i dont know windows , is it possible being a human and not knowing windows?

  • @apenaswellignton
    @apenaswellignton 17 วันที่ผ่านมา

    Pog

  • @ramQi
    @ramQi 17 วันที่ผ่านมา

    the references link part is being talk at 1:07:43

  • @ilikebananas342
    @ilikebananas342 17 วันที่ผ่านมา +1

    18:45 'const' may not work but 'static const' should be fine here, even in C99

    • @ElPikacupacabra
      @ElPikacupacabra 13 วันที่ผ่านมา

      In fact, clang allows `const` as well.

  • @binitrupakheti4246
    @binitrupakheti4246 17 วันที่ผ่านมา +1

    What are your thoughts on Odin?

    • @tomwilliam7299
      @tomwilliam7299 17 วันที่ผ่านมา +1

      Very bad language

    • @vlc-cosplayer
      @vlc-cosplayer 16 วันที่ผ่านมา +1

      Femboy god (Thor and Loki crossdressed too lmao)

  • @patfre
    @patfre 17 วันที่ผ่านมา +1

    Is anyone going to mention the text at the bottom?

    • @korigamik
      @korigamik 17 วันที่ผ่านมา +3

      Always been there

    • @RandomGeometryDashStuff
      @RandomGeometryDashStuff 14 วันที่ผ่านมา

      tea temperature looks like broken

  • @dastan331
    @dastan331 17 วันที่ผ่านมา

    Command pattern

  • @neshkeev
    @neshkeev 17 วันที่ผ่านมา

    Why not to use OpenGL/Vulcan instead of Raylib?

    • @TiaguinhouGFX
      @TiaguinhouGFX 17 วันที่ผ่านมา +3

      Raylib is an abstraction on top of OpenGL/Vulkan/Metal/Direct3D

    • @rogo7330
      @rogo7330 16 วันที่ผ่านมา +2

      Because he already used OpenGL and said that it sucks to literally copypaste the same stuff again and again and go through the same bugs again and again instead of just asking library "give me a window" and "draw rectangle there and flip the screen". Vulcan is just more annoying OpenGL that sucks less in terms of API and performance, but he literally draw a teapot in browser at 60 fps only with his old-ass Intel-i3 10-years old laptop's CPU.

  • @TurtleKwitty
    @TurtleKwitty 14 วันที่ผ่านมา

    You should really be assigning the target to the value in your teardown rather than the value + offset to eliminate any float weirdness/accumulated errors

  • @CHEL0VEK100
    @CHEL0VEK100 17 วันที่ผ่านมา +1

    detachaudiostreamprocessor

  • @apurbosarker112
    @apurbosarker112 17 วันที่ผ่านมา

    The C god is here

  • @user-ip6yp1qu6g
    @user-ip6yp1qu6g 17 วันที่ผ่านมา +1

    Возможно глупый вопрос, но я наблюдаю за твоим каналом долгое время и знаю, что ты проводил стримы на русском.
    А почему нет видео на русском?
    Очень мало людей, которые с такой же глубиной разбирались в С и записывали интересные видео

    • @__gadonk__
      @__gadonk__ 17 วันที่ผ่านมา

      I think he just wants to reach an international audience. The russian stream was just an April fools joke

    • @rogo7330
      @rogo7330 16 วันที่ผ่านมา

      But why not in english? If you have trouble understanding what he says, then this is perfect opportunity to improve on the language. It just more accessible for everybody to speak and write something in one well-understood language (in 21 centuary it's English, like it or not). If streams will be only in Russian you will get people who speak Russian, not people who searched for C programming, neural networks programming, Ada programming... I hope I stated my point clear.

  • @RandomGeometryDashStuff
    @RandomGeometryDashStuff 14 วันที่ผ่านมา

    02:54:27 why no `data->tasks.capacity = 0;`?
    edit: duplicate of chat 02:57:53

  • @victordvickie
    @victordvickie 15 วันที่ผ่านมา

    !still grep after coogle

  • @blackhaze3856
    @blackhaze3856 17 วันที่ผ่านมา

    Does he say he heard about coroutines and goroutines and doesn't know what they mean? He made a chat in Go over 6 months ago. 😮

  • @tcocaine
    @tcocaine 16 วันที่ผ่านมา

    If you haven't implemented an async engine, a heap-allocated state machine with an event loop (optionally with thread pools), then you don't understand async.

  • @aredrih6723
    @aredrih6723 17 วันที่ผ่านมา +1

    1:04:55 I think Scratch is alright to get a feel for what a tokenizer and lexer value in the code. When you start learning, word on a screen don't look like declarations, loops and conditionals, they look like words.
    I don't think there is anything wrong with training wheels to get a feel, but it is really hard to judge how hard a problem is in programming so you end up with people on children bike trying to do some cyclocross. Yeah it's hard, but I would not blame the bike on that one, but you should not advertise it as possible when giving the bike.
    That said, I doubt you get much values after a few weeks of Scratch.

    • @boccobadz
      @boccobadz 17 วันที่ผ่านมา

      Honestly, back in the day we had stuff like that - Logo programming language. It put off more people than I can think of.

  • @ladyattis
    @ladyattis 17 วันที่ผ่านมา +1

    I think scratch and other visual languages tend to make mistakes that making it all look like blocks you can attach supposedly improves comprehension. In reality, the only time I've seen such visualization be useful is to grasp the problem like a flow chart (aka state machine). Once you got your chart or graph of possible states then it's better to go straight to a textual interface for implmentation and debugging unless what you're doing has an actual visual output like your squares were. But that doesn't need to be integrated into the development platform which Scratch forces on users. I love smalltalk and I think it's a great first language but I'll be damned if anyone wants to force lego-brick clone UIs on me. If you're gonna do that then copy VB and HyperCard (which VB and other RAD tools copied from), don't settle for less.

  • @danielschmitt5750
    @danielschmitt5750 17 วันที่ผ่านมา

    Nice implementation of the composite pattern. I know you don't think much of design patterns but it is a quite elegant solution nevertheless.

  • @JoshuaF.
    @JoshuaF. 17 วันที่ผ่านมา +3

    cool

    • @lowlevelcodingch
      @lowlevelcodingch 17 วันที่ผ่านมา

      JOSHUA? WTF

    • @JoshuaF.
      @JoshuaF. 17 วันที่ผ่านมา +1

      @@lowlevelcodingch wild Joshua caught in the wild

    • @alexnik1181
      @alexnik1181 17 วันที่ผ่านมา

      cool

  • @lowlevelcodingch
    @lowlevelcodingch 17 วันที่ผ่านมา +4

    I'm Async Too!

  • @RandomGeometryDashStuff
    @RandomGeometryDashStuff 14 วันที่ผ่านมา

    01:35:41 what if malloc fail?

  • @kala-telo
    @kala-telo 15 วันที่ผ่านมา +1

    I disagree with statement about scratch at 1:04:41, actually, I got interested in programming 10-11 years ago, when I was 6 or 7, basically after I learned how to read, I was bored and found scratch installed on family PC. I did some simple program which reacted to key presses, showed my dad and he gave me some scratch course back.
    I'd say scratch is better than most "real" programming languages in sense that it's
    1) international, I didn't know english back then when I was 6
    2) doesn't require typing, at this age, inserting code letter by letter, at speed about 1 word per minute is boring as hell
    Also you would do cool things without variables and functions, since most of values stored in objects(so called sprites) themselves. For example you are not doing `cat.x = cat.x + 10`, but `cat.move(10)`. You will eventually encounter variables, but scratch doesn't just trow them at your face.
    Well, at least it was my experience with it

  • @metala
    @metala 17 วันที่ผ่านมา +1

    nob it will grow indefinitely and then it would replace systemd.

  • @curly35
    @curly35 16 วันที่ผ่านมา

    i don't understand where he made his task loop async? i was waiting to see how that would be implemented and then it just magically worked with totally sync looking code...

  • @GegoXaren
    @GegoXaren 17 วันที่ผ่านมา

    I love Russians/Slavs doing a fake Russian/Slav accent.

  • @el_chivo99
    @el_chivo99 4 วันที่ผ่านมา

    you should develop python like generators from scratch

  • @r2com641
    @r2com641 17 วันที่ผ่านมา

    🇦🇿

  •  17 วันที่ผ่านมา

    first

    • @Kobold666
      @Kobold666 17 วันที่ผ่านมา

      firster

    • @tomwilliam7299
      @tomwilliam7299 17 วันที่ผ่านมา

      Shame on your ass 🤣🤣🤣