chapter 27 - interlude: thread API Flashcards

1
Q

How do you create a new thread in POSIX?

A

Use pthread_create

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

What does the fourth argument of pthread_create() do?

A

It passes a void* argument to the new thread’s function - allowing any type of data to be passed

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

How do you wait for a thread to finish in POSIX?

A

Use pthread_join(pthread_t thread, void **retval);

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

Why should you not return a pointer to a stack variable from a thread?

A

Because the stack is destroyed when the thread ends — leading to undefined behavior

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

What should you pass to pthread_join() if you don’t care about the return value?

A

NULL

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

What functions are used to protect critical sections in pthreads?

A

pthread_mutex_lock()

pthread_mutex_unlock()

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

What is pthread_mutex_trylock() used for?

A

It tries to acquire the lock without blocking — returns immediately if the lock is bus

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

What is pthread_mutex_timedlock() used for?

A

Waits for a lock up to a specified timeout

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

What is a condition variable used for in pthreads?

A

To allow threads to wait for a condition to become true — enables signaling between threads

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

What do you need to include and link to use pthreads in C?

A

Include <pthread.h></pthread.h>

Compile with the -pthread flag

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