gpt4 book ai didi

c++ - GetProcessID 与 GetThreadID

转载 作者:可可西里 更新时间:2023-11-01 09:44:45 28 4
gpt4 key购买 nike

当我执行下面的代码时——:

#include<iostream>
#include<windows.h>
using namespace std;
int main()
{
cout << GetCurrentProcessId() << endl;
cout << GetCurrentThreadId() << endl;
}

我得到以下输出-:

1160
4316

我的问题是-:

  • 既然这是一个独立的进程,它怎么会有“thread-id”?
  • 为什么输出中的两个整数不同?

我在 Windows 7 上使用 C++14 和 TDM-GCC 4.9.1 编译器。

最佳答案

由于您使用的是 Windows,因此 MSDN 必须这样说:

Processes and Threads

An application consists of one or more processes. A process, in the simplest terms, is an executing program. One or more threads run in the context of the process. A thread is the basic unit to which the operating system allocates processor time. A thread can execute any part of the process code, including parts currently being executed by another thread.

About Processes and Threads

Each process provides the resources needed to execute a program. A process has a virtual address space, executable code, open handles to system objects, a security context, a unique process identifier, environment variables, a priority class, minimum and maximum working set sizes, and at least one thread of execution. Each process is started with a single thread, often called the primary thread, but can create additional threads from any of its threads.

A thread is the entity within a process that can be scheduled for execution. All threads of a process share its virtual address space and system resources. In addition, each thread maintains exception handlers, a scheduling priority, thread local storage, a unique thread identifier, and a set of structures the system will use to save the thread context until it is scheduled. The thread context includes the thread's set of machine registers, the kernel stack, a thread environment block, and a user stack in the address space of the thread's process. Threads can also have their own security context, which can be used for impersonating clients.

关于c++ - GetProcessID 与 GetThreadID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32123473/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com