Pthreads Flashcards

1
Q

Creating a pthread_t

A

To create a thread in Pthreads, you use the pthread_create function, which has the following syntax
pthread t*
const pthread_attr_t* atrr_p
void* (*start_routine)(void*)
void* arg_p

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

Stopping a pthread_t

A

A single call to pthread_join will wait for the thread associated with the pthread_t object to complete. The syntax of pthread join is

pthread t* thread
void** ret_val_p

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

Mutex

A

A mutex (short for mutual exclusion) is a synchronization primitive used to protect shared resources from concurrent access, ensuring that only one thread accesses a critical section at a time.

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

Initialising a Mutex

A

To use a mutex, you first need to initialize it using pthread_mutex_init:

```c
pthread_mutex_t mutex;
const pthread_mutexattr_t* attr
pthread_mutex_init(&mutex, NULL);
~~~

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

Locking and Unlocking a Mutex

A

To enter a critical section, a thread must lock the mutex. After finishing the critical section, the thread must unlock the mutex.

```c
pthread_mutex_lock(&mutex);
// Critical section
pthread_mutex_unlock(&mutex);
~~~

NB: When pthread_mutex_lock() is called and mutex is already locked, it becomes a blocking call.

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

Destroying a Mutex

A

When a mutex is no longer needed, it should be destroyed to free resources.

```c
pthread_mutex_destroy(&mutex);
~~~

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

Semaphore

A

Semaphores are synchronization tools used to control access to a resource by multiple threads. Unlike mutexes, semaphores can allow more than one thread to access a critical section simultaneously, based on the semaphore’s value.

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

Initialising a Semaphore

A

To initialize a semaphore, you use the sem_init function, which has the following arguments:

|Type|Variable Name|Comment|

|sem_t*|sem|/* out */|

|int|pShared|/* in */|

|unsigned int|value|/* in */|

```c
#include <semaphore.h>
sem_t semaphore;
sem_init(&semaphore, 0, 1); // 1 is the initial value
~~~</semaphore.h>

  • pShared = 0 implies the semaphore is shared among threads of the same process.
  • if pShared != 0, the semaphore is shared between DIFFERENT processes.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Waiting and Posting a Semaphore

A

To decrease the semaphore value (wait operation), a thread uses sem_wait. To increase the semaphore value (post operation), a thread uses sem_post.

```c
sem_wait(&semaphore);
// Critical section
sem_post(&semaphore);
~~~

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

Destorying a Semaphore

A

When a semaphore is no longer needed, it should be destroyed.

```c
sem_destroy(&semaphore);
~~~

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

Read-Write Locks

A

Read-write locks allow multiple threads to read from a shared resource concurrently while still providing exclusive access for writing.

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

Why Lock for Reading?

A

Locking for reading ensures that data being read is consistent and not being modified by another thread during the read operation, preventing race conditions and maintaining data integrity.

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

Why Lock for Writing

A

Locking for writing provides exclusive access to the shared resource, preventing data corruption and ensuring that updates are correctly applied without interference from other threads.

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

Initializing a Read-Write Lock

A

To initialize a read-write lock, use pthread_rwlock_init with the following arguments:

|Type|Variable Name|Comment|

|—|—|—|

|pthread_rwlock_t*|rwlock|/* out */|

|const pthread_rwlockattr_t*|attr|/* in */|

```c
pthread_rwlock_t rwlock;
pthread_rwlock_init(&rwlock, NULL);
~~~

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

Using Read-Write Locks

A

To lock the resource for reading, use pthread_rwlock_rdlock. To lock it for writing, use pthread_rwlock_wrlock. After accessing the resource, unlock it with pthread_rwlock_unlock.

```c
pthread_rwlock_rdlock(&rwlock);
// Reading critical section
pthread_rwlock_unlock(&rwlock);
pthread_rwlock_wrlock(&rwlock);
// Writing critical section
pthread_rwlock_unlock(&rwlock);
~~~

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

Destroying a Read-Write Lock

A

When the read-write lock is no longer needed, it should be destroyed.

```c
pthread_rwlock_destroy(&rwlock);
~~~