Why is there a “dwThreadId” and a “dwProcessId” for a newly created process via CreateProcess in win32 API programming?

StackOverflow https://stackoverflow.com/questions/12759861

  •  05-07-2021
  •  | 
  •  

سؤال

I'm curious why there are two separate IDs - dwProcessId and dwThreadId returned in the PROCESS_INFORMATION data structure when I call CreateProcess in win32 API programming?

In what cases would I use the dwThreadId? So far I haven't found a use case. I only use the process id to identify the program that I started via CreateProcess.

Also I'm very curious why does Linux have just a pid (aka ProcessId) but Windows has both pid and threadid?

هل كانت مفيدة؟

المحلول

Every process has at least one thread. The thread id gives you the id of the first thread created for a process by the CreateProcess API. You can create more threads and they'd have id's too.

They're separate only because process ids are for processes and thread ids are for threads. I don't think there's much more to say about it!

نصائح أخرى

Created process has threads, because any process has at least one thread. So creating a process you also create its first thread of execution, and the API function get you both at once.

You use thread identifiers when you need to lookup certain thread, and you use process identifier where your called function or API expect an identifier of a process. You are unlikely to stumble on anything that takes both and you are in doubt which of the identifiers to hand in there.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top