多线程常见面试问题
- What is difference between a thread and a process?
- When would you multithread a program? Why would you multithread a program?
- How will you create a POSIX thread?
- Can you join a detachable thread?
- Give a scenario in which you will create a detachable thread?
- What resources are shared by threads of a process?
- How can you create thread specific data?
- How will you pass multiple arguments to a thread?
- How can you kill a thread?
- How to provide mutual exclusive access on shared resources, to threads?
- What happens if your main thread terminates before termination of child thread? How can you prevent a thread becoming a “zombie” ?
- How will you determine status of a thread?
- When to perform thread cancellation and not thread kill?
- How can you prevent deadlocks happening in multithreaded programs?
- 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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现