Doesn't using std::atomic to synchronize like this resemble a spinlock? Can that trully be called lock-free? With a large number of threads, the likelihood of the running thread being the correct one is low. So wouldn't it be more prudent to use blocking primitives like semaphores/condition variables? On another note, since we continuously spin and will eventually read the updated value, can't we use a non-thread-safe integer instead of std::atomic in this scenario?
@@abhi-5783 I understand now that we can't use a non-thread-safe integer because of UB. The memory ordering also ensures the proper value of `currIndex` is read. However, I still don't understand how this "lock-free" approach makes any sense in this scenario. Especially if we are expecting a large number of threads, or we are doing slow operations like writing to and flushing std::cout. Isn't this just a bad use-case for atomics?
This is very useful. Thank you very much. May I request you to post a video on ‘how market data handlers work and update order book in HFT firms’ please? Thanks
I don't understand why multithreading is relevant for this problem. Only one thread is doing work at a particular time, while others are waiting for their turn. For learning purposes, it is fine but it would be better if a real use case is chosen.
these kind of questions are asked to check if a candidate knows about thread synchronization and is able to implement it. These concepts are very useful in systems which utilises the pipeline architecture for very high throughout, where the output of nth thread is input to (n+1)th thread.
Bhaiya, I would be highly grateful if you can kindly advise that, in your genuine experience, do ranks on competitive programming profiles help in getting shortlisted for interview?? Even as Candidate Master in Codeforces or 6* in CC... I'm nit grad this year, but asking in respect of off campus & not particularly for hfts. Thanks bhaiya immensely!🙏🙏
Unless you prove yourself in those coding rounds or hackathons - conducted by companies, no one would care about your ranks. This is not like JEE Advanced exams, where people get selected based on ranks.
ignore the sound of AC in background. Didn’t realise that it was so loud. Apologies 🙏
The AC noise is not an issue, Great Content!
Just one question - Why write lock-free programs? What is the use case here?
Doesn't using std::atomic to synchronize like this resemble a spinlock? Can that trully be called lock-free?
With a large number of threads, the likelihood of the running thread being the correct one is low. So wouldn't it be more prudent to use blocking primitives like semaphores/condition variables?
On another note, since we continuously spin and will eventually read the updated value, can't we use a non-thread-safe integer instead of std::atomic in this scenario?
No, using std atomic ensure sequential consistency, which ensures that the compiler doesn’t do reordering or read old cached value.
@@abhi-5783 I understand now that we can't use a non-thread-safe integer because of UB. The memory ordering also ensures the proper value of `currIndex` is read.
However, I still don't understand how this "lock-free" approach makes any sense in this scenario. Especially if we are expecting a large number of threads, or we are doing slow operations like writing to and flushing std::cout. Isn't this just a bad use-case for atomics?
Thank you. Your approach and code is clean and elegant. Please create similar videos like this whenever you get time...
✌️
Shubham I always enjoy your videos quite informative being a SDE still lots of Things i have to learn concurrency ,,, Great
✌️
This is very useful. Thank you very much. May I request you to post a video on ‘how market data handlers work and update order book in HFT firms’ please? Thanks
I don't understand why multithreading is relevant for this problem. Only one thread is doing work at a particular time, while others are waiting for their turn. For learning purposes, it is fine but it would be better if a real use case is chosen.
these kind of questions are asked to check if a candidate knows about thread synchronization and is able to implement it. These concepts are very useful in systems which utilises the pipeline architecture for very high throughout, where the output of nth thread is input to (n+1)th thread.
I've a question is it ok to use global variables while using this programming techniques
yes please post videos regularly
✌️
the while loop is technically a lock
Bhaiya, I would be highly grateful if you can kindly advise that, in your genuine experience, do ranks on competitive programming profiles help in getting shortlisted for interview?? Even as Candidate Master in Codeforces or 6* in CC... I'm nit grad this year, but asking in respect of off campus & not particularly for hfts. Thanks bhaiya immensely!🙏🙏
Unless you prove yourself in those coding rounds or hackathons - conducted by companies, no one would care about your ranks. This is not like JEE Advanced exams, where people get selected based on ranks.
Bhai what would be chances of getting selected in hft for CP vs Non CP but good skills