简体   繁体   English

minGW 中的线程 C 程序

[英]Threaded C programs in minGW

I am creating a program that intercepts all packets when a certain link is down.我正在创建一个程序,当某个链接关闭时拦截所有数据包。 I would need to implement the sniffer and the link-checker as threads.我需要将嗅探器和链接检查器实现为线程。 But minGW does not have pthreads.但是 minGW 没有 pthread。

How do you implement threads in minGW?你如何在minGW中实现线程?

Edit:answer编辑:回答

http://www.codeproject.com/KB/threads/sync.aspx http://www.codeproject.com/KB/threads/sync.aspx

Vivek Goel led me to this (_beginthread). Vivek Goel 将我带到了这个 (_beginthread)。 Both examples compile on Code::blocks/minGW!两个示例都在 Code::blocks/minGW 上编译!

MinGW doesn't provide a full POSIX model. MinGW 不提供完整的 POSIX 模型。 If you want threads in the standard package, you'll have to use the Windows variety.如果您想要标准包中的线程,则必须使用 Windows 版本。

It states on the MinGW main page :它在 MinGW主页上声明

MinGW compilers provide access to the functionality of the Microsoft C runtime and some language-specific runtimes. MinGW 编译器提供对 Microsoft C 运行时和一些特定于语言的运行时功能的访问。 MinGW, being Minimalist, does not, and never will, attempt to provide a POSIX runtime environment for POSIX application deployment on MS-Windows. MinGW 是 Minimalist,不会也永远不会尝试为 MS-Windows 上的 POSIX 应用程序部署提供 POSIX 运行时环境。 If you want POSIX application deployment on this platform, please consider Cygwin instead.如果您希望在此平台上部署 POSIX 应用程序,请考虑使用 Cygwin。

Cygwin does have pthreads support because it provides the Cygwin DLL, an emulation layer, whereas MinGW is more gcc for use with the Windows way of doing things. Cygwin确实有 pthreads 支持,因为它提供了 Cygwin DLL,一个仿真层,而 MinGW 更像是 gcc,用于与 Windows 的做事方式一起使用。

Alternatively, if Cygwin isn't an option, you can look into Pthreads/Win32 which claims to work with MinGW.或者,如果 Cygwin 不是一个选项,您可以查看声称可以与 MinGW 一起使用的Pthreads/Win32

With MinGW you have some options.使用 MinGW,您有一些选择。 My recomendations:我的建议:

  1. Use native Windows API to make your threads.使用本机 Windows API 来制作您的线程。

  2. Use a good library to manage that.使用一个好的库来管理它。 I usually use a C++ framework called JUCE to have a better life.我通常使用一个名为 JUCE 的 C++ 框架来过更好的生活。

Using Windows API you could try something like this:使用 Windows API,你可以尝试这样的事情:

/*
 *  main.c
 *
 *  Created on: 18/10/2011
 *  Author: Cesar Carlos Ortiz Pantoja.
 */

#include <windows.h>
#include <stdio.h>

int exitCondition;

struct threadParams{
    int param1;
    int param2;
};

static DWORD WINAPI myFirstThread(void* threadParams)
{
    struct threadParams* params = (struct threadParams*)threadParams;

    while(exitCondition){
        printf("My Thread! Param1:%d, Param2:%d\n", params->param1, params->param2);
        fflush(stdout);
        Sleep(1000);
    }

    return 0;
}

int main(void){
    DWORD threadDescriptor;
    struct threadParams params1 = {1, 2};
    exitCondition = 1;

    CreateThread(
        NULL,                   /* default security attributes.   */
        0,                      /* use default stack size.        */
        myFirstThread,          /* thread function name.          */
        (void*)&params1,        /* argument to thread function.   */
        0,                      /* use default creation flags.    */
        &threadDescriptor);     /* returns the thread identifier. */

    while(1){
        printf("Main Program!\n");
        fflush(stdout);
        Sleep(2000);
    }

    return 0;
}

Regards问候

您必须使用 WIN 32 线程 API,请参阅http://www.mingw.org/wiki/Use_the_thread_library http://msdn.microsoft.com/en-us/library/ms684254(v=vs.85).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM