thread Flashcards

1
Q

What option do we need to compile thread application?

A

-pthread

Ex: g++ -pthread -g -o hello.out hellp.cpp

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

How many way can we construct a thread?

A

A function pointer

A function object

A lambda expression

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

How to get current thread id?

A

this_thread::get_id()

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

How to create atomic variable?

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

How to update atomic variable and make sure other did not update that value?

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

How to make cout thread-safe?

A

cout.sync_with_stdio(true);

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

How and Why using thread local?

A

Each thread can have its own static variable

static thread_local int mismatch_count;

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

What is the lock_guard?

A

The class lock_guard is a mutex wrapper that provides a convenient RAII-style mechanism for owning a mutex for the duration of a scoped block.

When a lock_guard object is created, it attempts to take ownership of the mutex it is given. When control leaves the scope in which the lock_guard object was created, the lock_guard is destructed and the mutex is released.

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

How condition_variable with “wait” method work?

A

before calling “wait”, the thread should already acquired that lock. When “wait” execute, it will call unlock() then block the thread

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

What is the difference between unique_lock and lock_guard?

A

We can construct unique_lock without acquire the mutex by passing defered_lock and call lock later.

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

what is call_once used for?

A

to make sure that the function will get called only one time, no matter how many threads try to call the same call_once().

We used it for initialization with multi-thread

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

Can we copy unique_lock?

A

No but we can move

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

Can we call std::async and not create a thread?

A

Yes, by passing std::launch::deferred

auto fut = async(launch::deferred, process_something);

auto x = fut.get(); // create thread this time.

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

How to use async instead of thread, cond, mutex

A

the following code will create 10 threads that execute “divide” function and return value will keep in “future” object that store in vector all_tasks.

We can get the result later.

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

How to use promise to send data to child thread?

A
  1. on the caller, create promise object then get future object from that
  2. pass that future object in async
  3. on the function parameter, declare future object
  4. in that function (child thread) call future object to get value
  5. on the caller later one then call promise object “set_value”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly