Condition Variables Flashcards

1
Q

What are

pthread Condition Variables?

A
  • Not really a variable
    • Does not contain your program’s data
  • Works basically as a list of threads
  • A means of allowing threads to:
    • Wait to be signaled(woken up) by another thread
    • Send a signal to other waiting threads
  • Specified by the type:

pthread_cond_t

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Pthread Conditional Variables:

Important Types

Important Functions

A

Important Types

  • pthread_cond_t
  • pthread_mutex_t

Important Functions (parameters omitted)

  • Initialize: pthread_cond_init()
  • Signal: pthread_cond_signal()
  • Wait : pthread_cond_wait()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

pthread Conditional Variables:

Signaling a condition

A

pthread_cond_signal( pthread_cond_t *myCond);

  • This call “wakes up” threads that are suspended and waiting for myCond
  • If multiple threads are waiting, only one will be able to unlock it’s mutex and execute
  • The selection of which thread gets the lock is arbitrary
  • No mutex variable is passed, but the calling thread must have exclusive access to myCond when calling
    • Lock the associated mutex variable before calling
    • Unlock mutex variable after calling
  • The calling thread is not suspended
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Problems with

Basic Threads

A
  • The process of creating a thread to perform a task, then joining with it(to synchronize completion) is ok for some problems, but too restrictive for others
  • Creating a thread is too expensive
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How pthread is

different from basic threads

A

Basic threads are created as needed.

  • With pthread, we create a set of threads all at once.
  • The threads are kept in a pool, ready to be allocated to tasks.
  • This minimizes overhead.
  • This is similar to the way the OS allocates processors to processes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

pthread

Conditional Variable Type:

Overview and Use

A

pthread_cond_t

  • Basically a list of threads waiting to be signaled(woken up)
  • Declare it as a global variable, or
    • allocate it on the heap
  • Must be shareable by all threads
  • Must initialize before use:

pthread_cond_init(

pthread_cond_t* someCondVar);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Relationship between

Conditional Variables

and

Mutexes

A
  • In most cases, you should protect access to a conditional variable, and the shared data associated with it
  • This is accomplished by the use of a

pthread_mutex_t

  • A thread should not be able to view or modify your shared data or condition variable unless it has exclusive access to it.
  • The mutex protects access to the conditional variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

pthread Conditional Variables:

Waiting for a Condition

A

pthread_cond_wait( pthread_cond_t* myCond,

pthread_mutex_t* mutex);

  • Adds the calling thread to the list of threads waiting for myCond
  • Suspends calling thread until “myCond” signaled
  • Associated mutex should already be locked
    • a function unlocks it after adding thread to myCond and suspending thread
  • When the thread returns from waiting, the mutex will be locked for the thread again.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly