I would like to implement an application in C/C++ that runs on Windows and Linux-based OS, the application will be multithreaded so after some research I found there are separate library methods for Windows and Linux respectively.
HANDLE CreateThread( // Windows
LPSECURITY_ATTRIBUTES lpThreadAttributes,
SIZE_T dwStackSize,
LPTHREAD_START_ROUTINE lpStartAddress,
__drv_aliasesMem LPVOID lpParameter,
DWORD dwCreationFlags,
LPDWORD lpThreadId
);
// Linux
int pthread_create( pthread_t* thread,
const pthread_attr_t* attr,
void* (*start_routine)(void* ),
void* arg );
According to Microsoft's Documentation, CreateThread
seems to use a virtual addressing for the thread's execution, which in my understanding this thread is a "User-level" Thread.
While pthread_create
on the other hand, from an answer to a similar question here,
"For example, every single process in a Linux system is a "kernel thread". And every user-created pthread is ALSO implemented as a new kernel thread."
So I suppose pthread_create
does create a kernel-based thread.
Comparing User-level threads and Kernel-level threads, I have came across an article / tutorial on this topic in Tutorial Point:
(This image comes from that page.)
Which means User-level threads from the same process will most likely share a Kernel-level thread. (This isn't multi-threading but parallel execution?)
As I want my application to have the same behavior (performance or execution wise) across both platforms, there is 2 question that I want to ask:
- Whether the thread created from
CreateThread
maps 1:1 to a kernel thread? (Which means the application can utilize multi-core processing.) - If the answer to Question (1) is false, are there any methods that equates or emulates the behavior of
pthread_create
in Windows platform? (There is aPsCreateSystemThread
for Windows but I am not sure whether the thread it creates has a similar if not higher privilege of operation compares to the one frompthread_create
)