How to write a Concurrent Thread-safe Queue from scratch?

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 พ.ย. 2024

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

  • @prabhavdogra6567
    @prabhavdogra6567 ปีที่แล้ว +4

    Noticed that in this video where we are enqueue'ing 1e6 number and dequeuing those 1e6 inserted numbers won't actually result in queue size being 0 at the end
    Because we are doing wgE and wgD together, if the dequeue operations happen more rapidly than enqueue operations then that can lead to queue size at the end being > 0

  • @sachinpandhare5143
    @sachinpandhare5143 ปีที่แล้ว +1

    One more important(and necessary) feature to add in thread safe queue is waiting and notification using condition variables!
    So that consumer threads will wait until there is something to read! And producer threads will wake up consumers when they enqueue something

  • @frikishaan
    @frikishaan ปีที่แล้ว +5

    I wish I had discovered this channel during my engineering.

  • @rohithuppala
    @rohithuppala ปีที่แล้ว +2

    It’s a nice intro but there are caveats. For example, batch processing which you mentioned in the example, it’s better to use multiple processes and leverage parallelism than managing a complex thread safe code. Considering compute is quite cheap, parallelism >>> concurrency unless exceptions
    And also there are alternatives to muteness where we leverage hardware locks using atomic variables in c++ and that’s more preferred compared to mutex. Sure, it’s not a magic bullet but definitely it has its own pros

  • @AkashMondal-xt2te
    @AkashMondal-xt2te ปีที่แล้ว +3

    Seeing how you connect your previous videos to current ones is great. Please make a video on TCP Queue. It would make an awesome connecting video. I have done the implementation on Rust, eager to see how you implement in Go and what is your thought process when designing it.

    • @AsliEngineering
      @AsliEngineering  ปีที่แล้ว +1

      Thanks for suggesting. Added it to my list to topics to cover.

  • @arpanghoshal2579
    @arpanghoshal2579 ปีที่แล้ว +2

    This was nice, I use elixir which is a functional language so their is no mutable shared state and therefore there are no problems related to thread safety. Every process gets its own copy of memory and works in isolation, processes exachange information via message passing. Its a different approach to solve thread based problems without using locks. Companies like whatsapp and discord are using it at sacle so it actaully works.

    • @bhumit070
      @bhumit070 ปีที่แล้ว

      That's why memory ussage goes 🚀

    • @arpanghoshal2579
      @arpanghoshal2579 ปีที่แล้ว

      It actually uses some tricks like persistent data structures to avoid copying memory.

  • @rushidesai2836
    @rushidesai2836 ปีที่แล้ว

    Your content is a gold mine.

  • @nithinsastrytellapuri291
    @nithinsastrytellapuri291 ปีที่แล้ว +2

    Hi Arpit, it would be great if you can explore the topic of lock free data structures

  • @rakshitgoyal8040
    @rakshitgoyal8040 ปีที่แล้ว +3

    Hi Arpit,
    Doesn't applying mutex on threading makes the program equivalent to a program running sequentially? Why should I use threading and mutex instead of simple sequential code in such scenarios?

    • @atharvaamte4812
      @atharvaamte4812 11 หลายเดือนก่อน

      Same doubt! Did you find answer?

    • @amritprakashiit-roorkee6942
      @amritprakashiit-roorkee6942 7 หลายเดือนก่อน

      I think it is more like the function will be added to the stack after the before function stops execution in the case of sequential code though in this case the function has already been loaded on to the stack and is just waiting for execution. This is my analogy, not sure though :)

  • @adhithyasrinivasan8022
    @adhithyasrinivasan8022 ปีที่แล้ว

    Please add a connecting video on how queue used inside kafka

  • @Delta-ee7fc
    @Delta-ee7fc ปีที่แล้ว

    I reallly love your videos! keep it up

  • @sounishnath513
    @sounishnath513 ปีที่แล้ว +3

    I have an doubt/Question ....
    Arpit - when we do use multithreading and mutexs, if I think on a high level it makes an "so called asynchronous program" to be act as a synchronised program?.
    When if it threads/routines with mutexes eventually making the execution synchronous on multiple workers how does it improving the performance and speeding up the execution in a multi processor environment??.
    May be i messed up so many questions into one single comment. would be helpful it you can help me to understand the background processes?.

    • @anindyasadhukhan7139
      @anindyasadhukhan7139 ปีที่แล้ว

      I have the same doubt.

    • @arpanghoshal2579
      @arpanghoshal2579 ปีที่แล้ว +4

      This is a very nice question and you are actually correct.
      Having locks essentially makes access to the data structure synchronous. The reason why locks are helpful is to avoid inconsistencies when multiple threads try to update the same memory location as shown in the video.
      When you use locks you might end up losing the performance benefits of concurrent programming since threads might be waiting on a lock

    • @shervilgupta92
      @shervilgupta92 ปีที่แล้ว +3

      Usually we only use locks in critical sections which can be a small set of instructions, imagine a scenario where we are fetching something from db thendoing some work on a shared variable. We'll only require the lock on this part where we are accessing the shared variable so the db calls can happen parallely thereby improving performance.

    • @rohithuppala
      @rohithuppala ปีที่แล้ว +2

      When your program is IO bound, use concurrency. If it’s not, use parallelism

    • @amitk6701
      @amitk6701 ปีที่แล้ว +1

      @@shervilgupta92 I think you nailed the answer when you mentioned 'critical section'.
      Coming back to questions:
      It takes some doing to design and code in a way that critical section is, first of all, identified & isolated and rest of things are structured around it. This 'critical section' needs safety. One way (easy and no-brainer) is to use 'pessimistic locking' as shown in video. In real world, it would cover most of the scenarios that one would work on, anything smart would definitely need a lot more scrutiny. You don't want call at middle of night due to that smartness.
      If your critical section is too big i.e. whole method then, yeah, its performance issue. You end up serializing whole thing and there won't be performance benefit. It may help to try things with thread safe data structures, immutability and so on. But there is no easy way out unless some restructuring is done on design and approach itself.

  • @sakshamsethi4123
    @sakshamsethi4123 ปีที่แล้ว

    Would love to see a performance comparison between atomic CAS based implementation and this lock based one

    • @AsliEngineering
      @AsliEngineering  ปีที่แล้ว +1

      Thanks for suggesting. Video on it dropping soon.

  • @pritamkumar6454
    @pritamkumar6454 ปีที่แล้ว

    Very informative 👏 😊

  • @shriyasundriyal3630
    @shriyasundriyal3630 ปีที่แล้ว +1

    Tried the same code and after enqueue , getting size of queue as 999987 instead of 1000000. Even after using Locks . Can someone please help me here ?

    • @shriyasundriyal3630
      @shriyasundriyal3630 ปีที่แล้ว +1

      Got the issue . Since I was trying to get the size of the queue just after the for loop of enqueue : there were still some elements to be inserted in the queue (These go routinues were still not scheduled on actual thread by go scheduler ) . Due to this we were getting incorrect queue size .
      `var i int32 = 0
      for i < NUM_THREADS {
      atomic.AddInt32(&i, 1)
      wgE.Add(1)
      go func() {
      queue.Enqueue(rand.Int31())
      wgE.Done()
      }()
      }
      fmt.Println("just after enqueue size:", queue.Size(), "....", i)
      fmt.Println("size:", queue.Size(), "....", i)`

  • @hwp438
    @hwp438 ปีที่แล้ว

    wow, insightful. Thankyou for the video

  • @singh.aadarsh
    @singh.aadarsh ปีที่แล้ว

    Keyboard sound is very irritating so please remove them in features video

  • @nishantketu2040
    @nishantketu2040 10 หลายเดือนก่อน

    Hi everyone if anyone can help me
    i have written a not a thread safe code but still its acting like thread safe can anyone help me here
    i tried to find the reason but not able to do so, if any one can explain
    here the code in python
    import threading
    import random
    class ConcurrentQueue:
    def __init__(self):
    self.queue = []
    def enqueue(self, item):
    self.queue.append(item)
    def size(self):
    return len(self.queue)
    NUM_THREADS = 1000000
    def main():
    queue = ConcurrentQueue()
    def enqueue_task():
    queue.enqueue(random.randint(1, 1000000))
    # Create and start enqueue threads
    enqueue_threads = [threading.Thread(target=enqueue_task) for _ in range(NUM_THREADS)]
    for thread in enqueue_threads:
    thread.start()
    # Wait for enqueue threads to finish
    for thread in enqueue_threads:
    thread.join()
    print("size:", queue.size())
    if __name__ == "__main__":
    main()

    • @AsliEngineering
      @AsliEngineering  10 หลายเดือนก่อน +2

      Python is single threaded. It is interleaving the execution and not really leveraging multiple cores to lead to sync issues in values.
      Would recommend switch it C, Java, or Go.

    • @nishantketu2040
      @nishantketu2040 10 หลายเดือนก่อน

      @@AsliEngineering thank you

  • @MaheshYoganand
    @MaheshYoganand ปีที่แล้ว

    👍🏼👍🏼

  • @pritamkumar6454
    @pritamkumar6454 ปีที่แล้ว

    Please reply i am asking this from last 2-3 days
    Hi arpit ,
    I work at startup didn't joined any college, i am self taught programmer , now also want to learn stuffs like complier, operating systems, can i learn it from resources available online or do i need to join college for this.
    😮

    • @AsliEngineering
      @AsliEngineering  ปีที่แล้ว +5

      you can learn everything online. just put in the required efforts.

    • @pritamkumar6454
      @pritamkumar6454 ปีที่แล้ว

      @AsliEngineering thank you so much
      One request please bring content on computer architecture I mean internals topics of computer science, everyone is bringing content like teaching frameworks, libraries these are very high level, you please do something different like advanced part of computer science
      Topics like
      Design of compilers,
      Design of operating system
      Everyone lacks in these parts

    • @harshpatel105
      @harshpatel105 ปีที่แล้ว +1

      @@pritamkumar6454 bruh go to collage

  • @CanThinkCanDo
    @CanThinkCanDo ปีที่แล้ว

    Ahh
    Devs training llm to outdate themselves of future jobs ✌️✌️
    Classic