Pthreads Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is a Mutex

A

Mutual Exclusion. Used to guarantee that one thread

“excludes” all other threads while it executes the critical section.

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

Issues with using Mutexes

A

Busy-waiting enforces the order threads access a critical section.

Using mutexes, the order is left to chance and the system.

There are applications where we need to control the order threads access the critical section.

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

What is a Sephamore?

A

A semaphore limits the number of simultaneous accesses allowed by incrementing and decrementing a counter with the wait (decrement) and signal (increment)

Semaphores are more powerful than mutexes since they can be initialized to any nonnegative value.

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

What is a Barrier?

A

A barrier is a point in a program at which the threads block until all of the threads have reached it.

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

Condition Variables

A

A condition variable is a data object that allows a thread to suspend execution until a certain event or condition occurs.

When the event or condition occurs another thread can signal the thread to “wake up.”

A condition variable is always associated with a mutex.

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

Processes and Threads

A

In Pthreads a process is the running instance of the program and a thread is a light weight process ( A sequence of statements within the program)

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

Starting threads

A

In Pthreads, the threads are started by the executable

Unlike in MPI where they are usually started by a script

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

Stopping the threads

A

We call the function pthread_join once for each thread.

A single call to pthread_join will wait for the thread associated with the pthread_t object to complete.

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

Matrix multiplication

A

Pthreads can perform matrix multiplication without critical sections

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

Read/Write lock

A

A read-write lock is used when it’s safe for multiple threads to simultaneously read a data structure, but if a thread needs to modify or write to the data structure, then only that thread can access the data structure during the modification.

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

Thread Safety

A

A block of code is thread-safe if it can be
simultaneously executed by multiple
threads without causing problems.

Some C functions cache data between
calls by declaring variables to be static,
causing errors when multiple threads call
the function.

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

Thread Unsafe Libraries

A

strtok
random
localtime

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

Shared vs Distributed Programming

A

A thread in shared-memory programming is analogous to a process in distributed memory programming.

However, a thread is often lighter-weight
than a full-fledged process.

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

Shared vs Private Variables

A

In Pthreads programs, all the threads have

access to global variables, while local variables usually are private to the thread running the function.

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

What is a Race Condition

A

When indeterminacy results from multiple threads attempting to access a shared resource such as a shared variable or a shared file, at least one of the accesses is an update, and the accesses can result in an error, we have a race condition.

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

Critical Section

A

A critical section is a block of code that updates a shared resource that can only be updated by one thread at a time.

So the execution of code in a critical section should, effectively, be executed as
serial code.

17
Q

Busy Waiting

A

Busy-waiting can be used to avoid conflicting access to critical sections with a
flag variable and a while-loop with an empty body.

It can be very wasteful of CPU cycles.

It can also be unreliable if compiler optimisation is turned on