Part 8 - Condition variables

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/8

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

9 Terms

1
New cards

What is a condition variable?

It is an explicit queue that threads can put themselves in and go to sleep when waiting for a condition to become desirable. Some other thread will wake all of the threads up whenever the variable changes its state.

2
New cards

Why is a condition variable more efficient than just using while loops to wait for a change in a variable?

Because a while loop wastes CPU cycles

3
New cards

What are the operations associated with condition variables in C?

- wait()

- signal()

4
New cards

What is an important thing to keep in mind when signaling or waiting a condition variable?

That you must hold a lock while doing it. This is to avoid race conditions.

5
New cards

What is the producer/consumer problem?

It is when you have one or more producers, and more than one consumer for a single buffer. When the buffer is full and the condition variable that says if the buffer is full or not is changed, the first consumer might be notified that it can now run to consume the buffer. But before that gets to run (because of the scheduler) the second consumer might run and consume everything. Suddenly when the first consumer finally gets to run there are nothing left in the buffer, which is not what it expects.

6
New cards

What is a recommendation to always use when dealing with condition variables in general?

While loops instead of if statements

7
New cards

How do you make sure that a consumer will only wake up producers (and vice-versa)?

By using two condition variables instead of a shared one

8
New cards

How can you assure that all threads waiting on a condition variable will be woken up?

By using broadcast() instead of signal()

9
New cards

How many threads does signal() wake up?

At least one (but usually only one)