简体   繁体   English

为什么linux线程函数在windows中有效?

[英]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.

相关问题 Integer:为什么它在 Linux 上工作而不在 Windows 上工作 - Integer: Why does it work on Linux and not on Windows 为什么linux编译的程序不适用于Windows - Why does a linux compiled program not work on Windows 为什么此OpenMP代码在Linux上却不能在Windows上运行? - Why does this OpenMP code work on Linux, but not Windows? 为什么我在C中创建的这个“读取文件”功能在Linux中的行为与在Windows中的行为不同? - Why does this “read file” function I've created in C behave differently in Linux than it does in Windows? 用于在文件中打印行数和列数的代码。 为什么它在Windows Mingw gcc环境下却不能在Linux上工作? - Code for printing Number of Lines and Columns in a File. Why does it work on Windows Mingw gcc environment but not on Linux? 为什么代码在Linux上而不在Windows上运行? - Why does the Code run on Linux but not on Windows? 为什么代码在Solaris上有效,但在Linux上无效? - Why does code work on Solaris but not on Linux? 为什么这段代码适用于Linux而不适用于SunOS? - Why does this code work on Linux but not on SunOS? 无法从线程执行带缓冲区的功能-在Linux上有效,在OS X上无效 - Function with buffer not executing from thread - works on Linux and does not on OS X 如何在Windows和Linux下链接到OS C库? - How does linking to OS C libraries under Windows and Linux work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM