多线程常见面试问题

  1. What is difference between a thread and a process?
  2. When would you multithread a program? Why would you multithread a program?
  3. How will you create a POSIX thread?
  4. Can you join a detachable thread?
  5. Give a scenario in which you will create a detachable thread?
  6. What resources are shared by threads of a process?
  7. How can you create thread specific data?
  8. How will you pass multiple arguments to a thread?
  9. How can you kill a thread?
  10. How to provide mutual exclusive access on shared resources, to threads?
  11. What happens if your main thread terminates before termination of child thread? How can you prevent a thread becoming a “zombie” ?
  12. How will you determine status of a thread?
  13. When to perform thread cancellation and not thread kill?
  14. How can you prevent deadlocks happening in multithreaded programs?
  15. How can you get results from a child thread in the main thread?

What is multithreading?

Multithreading is a technique to spawn multiple threads of a program. So what are threads? Classic definitions encountered everywhere says “threads are light weight process”. To explain, a thread is just a process( a running program ) with some difference. It is similar to a process where it executes a bunch of statements in a function, call other functions and do the defined task.

Thread v/s Process

A thread differs from a process in the following aspects:
Shares address space, global variables (data and heap segments)

Each thread has its own:

  • Stack area and stack pointer
  • Registers
  • Scheduling properties (such as policy or priority)
  • Signals (pending and blocked signals)
  • Thread specific data ( automatic variables )

A program written in any langugage including C C++ are single threaded. Such programs are extended to multithreaded capable when parallel processing is required. Most implementation provide a way to make it multithreaded. Some external libraries can be used for this purpose. We will use Pthread library to multithread a C program. Pthread library can be used with C++ also.

When to Multhithread

There can be many scenarios under which a program should be multhithreaded. But a criteria to do is when you find a sub task that can be done independent from execution of normal flow of a program.

A program can be multithreaded when there are some independent sub tasks, each thread performing this independent task and results are collated and acted upon. Some activity may not require the result. In such cases the thread performs the activity and then safely exits. Example would be some computing and then logging or when some notification is to be sent.

By doing independent tasks in parallel can enhance performance of the program and can effectively use CPU resources.

POSIX’s Pthread library interface

POSIX.1 specifies a set of interfaces (functions, header files) for threaded programming commonly known as POSIX threads, or Pthreads. Referring to the documentation, man pthreads, it says threads should share:

–  process ID
–  parent process ID
–  process group ID and session ID
–  controlling terminal
–  user and group IDs
–  open file descriptors
–  record locks (see fcntl(2))
–  signal dispositions
–  file mode creation mask (umask(2))
–  current directory (chdir(2)) and root directory (chroot(2))
–  interval timers (setitimer(2)) and POSIX timers (timer_create(2))
–  nice value (setpriority(2))
–  resource limits (setrlimit(2))
–  measurements of the consumption of CPU time (times(2)) and resources (getrusage(2))

Each thread would have its own:

–  thread ID (the pthread_t data type)
–  signal mask (pthread_sigmask(3))
–  the errno variable
–  alternate signal stack (sigaltstack(2))
–  real-time scheduling policy and priority (sched_setscheduler(2) and sched_setparam(2))
–  capabilities (see capabilities(7)) , under Linux
–  CPU affinity (sched_setaffinity(2)) , under Linux

原文地址:https://www.cnblogs.com/LUO77/p/5718066.html