Threads in C++ Flashcards

1
Q

Which version of C++ are threads available from?

A

include class is included in C++ 11.

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

Main thread

A

For every application there is a main thread running.

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

Threads

A

A thread is a path of execution within a process. A process can contain multiple threads.

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

Why multithreading?

A

A thread is also known as lightweight process. The idea is to achieve parallelism by dividing a process into multiple threads

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

Process vs Thread?

A

The primary difference is that threads within the same process run in a shared memory space, while processes run in separate memory spaces.
Threads are not independent of one another like processes are, and as a result threads share with other threads their code section, data section, and OS resources (like open files and signals). But, like process, a thread has its own program counter (PC), register set, and stack space.

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

How threads work in C++?

A

std: :thread t1(func, parameters);
t1. join()
t1. detach();

or
if(t1.joinable)
t1.join()

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

Mutex in C++

A

include

std::mutex m;
m.lock()
critical section
m.unlock();

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

Mutex try_lock()

A

tries to block the mutex. Returns true if the lock was free and granted else returns false immediately.
Non-blocking.

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

try_lock(m1, m2, m3,..)

A

Not an object of mutex, global function. Tries to block all the mutable objects passed to it.

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

Recursive Mutex in C++

A

Used to lock recursive methods.

std::recursive_mutex m1;

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

Timed Mutex in C++

A

Gives an option of locking the mutex for certain time period.

std: :timed_mutex m
m. try_lock_for(std::chrono::seconds(1))

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

lock guard in C++

A

Lightweight wrapper for owning mutex on scope basis.

std::lock_guard lock(m)

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

unique_lock in C++

A

Wrapper over mutex. You can have different locking strategies. Time constraint attempts at locking. Can notify threads waiting for thread.

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

future and promise in C++

A

std: :promise promise obj1;
std: :future obj2 = obj1.get_future();

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

std::async in C++

A

it runs a function asynchronously. and returns a std::future that will hold the value. You can specify the launch policy.

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

Condition Variable in C++

A
  1. Notify other threads.

2. Waiting for some condition.