Search code examples
cwindowslinuxpthreadscygwin

Why does a linux thread function work in windows?


I am compiling this program with gcc from cygwin package on my windows machine

#include<stdio.h>
#include<pthread.h>

void* thread_function(void)
{
    printf("hello");
}
int main(int argc,char *argv[])
{
    pthread_t thread_id[argc-1];
    int i;
    int status;
    printf("argc is %d ",argc-1);
    for(i=0;i<argc-1;i++)
    {
        pthread_create (&thread_id[i], NULL , &thread_function, NULL);
    }  

    for(i=0;i<argc-1;i++)
        pthread_join(thread_id[i],NULL);   
}

It compiles and creates a.exe

When I run it in cmd

./a.exe 2 3

The output is

argc is 2 hellohello

Question :

pthread.h and this thread function are related to linux os then why does it work with windows?


Solution

  • Beyond the pthread.h, cygwin is an entire unix-like enviroment, mainly based on gnu utilities, with some solaris stuff too.

    You can compile and use unix software inside the cygwin environment, but you can not distribute your exe file just like that. Every single windows machine would need to have installed and configure a cigwin environment.

    Cygwin apps are not 100% efficient or reliable on windows environment due to OS design differences. Just as windows apps are not 100% reliable or efficient when using wine, although Cygwin apps are compiled natively in windows.

    Other environment/libraries/compilers also provide ways for using posix functions. One of the most successful being Mingw, which provides libraries implemented on top of win32 calls and does not need an extra environment.

    On top of that Cygwin apps must comply with the GPL2 terms, so you must give your source code away with the app when using cygwin compiled binaries, Mingw runtime libraries are officially in the public domain, so you can keep your source code to yourself if you desire.

    http://www.mingw.org/
    http://en.wikipedia.org/wiki/MinGW
    http://en.wikipedia.org/wiki/Cygwin