JOIN ME
-----
RU-vid 🎬 / @cppnuts
Patreon 🚀 / cppnuts
COMPLETE PLAYLIST
------------
C++ Tutorial For Beginners: • Introduction To C++
STL (Standard Template Library): • STL In C++
ThreadIng In C++: • Multithreading In C++
Data Structures: • Data Structure
Algorithms: • Binary Search
Design Patterns: • Factory Design Pattern...
Smart Pointers: • Smart Pointer In C++
C++14: • Digit Separator In C++
C++17: • std string_view in C++...
C++ All Type Casts: • static_cast In C++
INTERVIEW PLAYLIST
------------
C++ Interview Q&A: • Structural Padding & P...
C++ Interview Q&A For Experienced: • How delete[] Knows How...
Linked List Interview Questions: • Find Kth Node From Bac...
BST Interview Questions: • Search Element In Bina...
Array Interview Questions: • Reverse An Array
String Interview Questions: • Check String Is Palind...
Bit Manipulation Questions: • Find Set Bit In Intege...
Binary Tree Interview Question: • Invert Binary Tree
Sorting Algorithms: • Bubble Sort
C++ MCQ: • Video
C MCQ: • What printf returns af...
C Interview Questions: • Designated Initializat...
QUICK SHORT VIDEOS
-------------
C++ Short : • C++ Short Videos
C Short : • Shorts C Programming MCQ
Producer Consumer OR Bounded Buffer Problem
THE PROBLEM STATEMENT:
1. Producer will produce and consumer will consume with synchronisation of a common buffer.
2. Until producer thread produces any data consumer thread can't consume.
3. Threads will use condition_variable to notify each other.
4. We need mutex if we use condition_variable because CV waits on mutex.
5. This is one of the example of producer consumer there are many.
PRODUCER thread steps:
1. lock mutex, if success then go ahead otherwise wait for mutex to get free.
2. check if buffer is full and if it is full then unlock mutex and sleep, if not then go ahead and produce.
3. insert item in buffer.
4. unlock mutex.
5. notify consumer.
CONSUMER thread steps:
1. lock mutex, if success then go ahead otherwise wait for mutex to get free.
2. check if buffer is empty and if it is, then unlock the mutex and sleep, if not then go ahead and consume.
3. consume item from buffer.
4. unlock mutex.
5. notify producer.
IMP:
Producer and Consumer have to notify each other upon completion of their job.
#threading #cpp #tutorial #programming #interviewquestions #softwareengineering #computerscience
5 окт 2024