27. Interlude: Thread API Flashcards

1
Q

Describe the POSIX interface for thread creation

A

pthread_create(pthread_t thread, const pthread_attr_t attr, void *(start_routine)(void), void *arg);

*thread - pointer to strcture of type pthread_t
*attr - attributes the thread might have (stack size, priority, etc.)
start_routine - a pointer to function which takes void pointer and return void pointer
*arg - function arguments

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

Why does thread creation call uses void argument and void return value for function?

A

It allows us to pass and return any type of data

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

How do you wait for thread to complete? Describe call interface

A

int pthread_join(pthread_t thread, void **value_ptr);

thread - specify the thread to wait for
**value_ptr - pointer to return value we expect to get back

**value_ptr is an address of a pointer (i.e pointer to pointer) where we will put the returned pointer from thread

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

What is interface for lock and unlock?

A

int pthread_mutex_lock(pthread_mutex_t *mutex);

int pthread_mutex_unlock(pthread_mutex_t *mutex);

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

What you need to do before using a lock?

A

It should be properly initialized. It can be done with following:
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
or
pthread_mutex_init(&lock, NULL) - where the second argument is an optional set of attributes, NULL stands for defaults

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

What should be done after using the lock completely?

A

pthread_mutex_destroy()

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

What are other interfaces that can be used to acquite the lock?

A

int pthread_mutex_trylock(pthread_mutex_t *mutex); - return failure if lock is already held

int pthread_mutex_timedlock(pthread_mutex_t *mutex, struct timespec *abs_timeout) - returns after timeout or after acquiring the lock

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

What are conditional variables used for?

A

For cases when some kind of signaling should take place between threads, if one thread is waiting for another to do something before it can continue

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