[英]Why does a linux thread function work in windows?
I am compiling this program with gcc from cygwin package on my windows machine 我正在用我的Windows机器上的cygwin包中的gcc编译这个程序
#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 它编译并创建一个.exe
When I run it in cmd 当我在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? pthread.h和这个线程函数与linux操作系统相关,那为什么它与windows一起工作?
They are not "Linux threads", they are P OSIX thread s. 它们不是“Linux线程”,它们是P OSIX 线程 。 Cygwin provides a POSIX compatibility layer, including
pthread.h
. Cygwin提供POSIX兼容层,包括
pthread.h
。
Beyond the pthread.h, cygwin is an entire unix-like enviroment, mainly based on gnu utilities, with some solaris stuff too. 除了pthread.h之外,cygwin是一个完整的类似unix的环境,主要基于gnu实用程序,还有一些solaris。
You can compile and use unix software inside the cygwin environment, but you can not distribute your exe file just like that. 您可以在cygwin环境中编译和使用unix软件,但是您不能像这样分发您的exe文件。 Every single windows machine would need to have installed and configure a cigwin environment.
每台Windows机器都需要安装和配置一个cigwin环境。
Cygwin apps are not 100% efficient or reliable on windows environment due to OS design differences. 由于操作系统设计的差异,Cygwin应用程序在Windows环境中效率不高或不可靠。 Just as windows apps are not 100% reliable or efficient when using wine, although Cygwin apps are compiled natively in windows.
正如Windows应用程序在使用葡萄酒时不是100%可靠或高效,尽管Cygwin应用程序是在Windows中本地编译的。
Other environment/libraries/compilers also provide ways for using posix functions. 其他环境/库/编译器也提供了使用posix函数的方法。 One of the most successful being Mingw, which provides libraries implemented on top of win32 calls and does not need an extra environment.
其中最成功的是Mingw,它提供了在win32调用之上实现的库,并且不需要额外的环境。
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. 最重要的是,Cygwin应用程序必须符合GPL2条款,因此在使用cygwin编译的二进制文件时必须使用应用程序提供源代码,Mingw运行时库正式在公共域中,因此如果您自己保留源代码,你渴望。
http://www.mingw.org/ http://www.mingw.org/
http://en.wikipedia.org/wiki/MinGW http://en.wikipedia.org/wiki/MinGW
http://en.wikipedia.org/wiki/Cygwin http://en.wikipedia.org/wiki/Cygwin
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.