minGW中的螺纹C程序

我正在创建一个程序,当某个链接关闭时拦截所有数据包。 我需要将嗅探器和链接检查器实现为线程。 但是minGW没有pthreads。

你如何在minGW中实现线程?

编辑:答案

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

Vivek Goel带我去了这个(_beginthread)。 这两个例子都在Code :: blocks / minGW上编译!

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

MinGW不提供完整的POSIX模型。 如果你想要标准包中的线程,你将不得不使用Windows品种。

它在MinGW 主页上说明 :

MinGW编译器提供对Microsoft C运行时function和某些特定于语言的运行时的访问。 MinGW是Minimalist, 它不会,也绝不会尝试为MS-Windows上的POSIX应用程序部署提供POSIX运行时环境。 如果您希望在此平台上部署POSIX应用程序,请考虑使用Cygwin。

Cygwin 确实有pthreads支持,因为它提供了Cygwin DLL,一个仿真层,而MinGW更像是用于Windows操作方式的gcc。

或者,如果Cygwin不是一个选项,你可以查看声称与MinGW一起使用的Pthreads / Win32 。

有了MinGW,你有一些选择。 我的推荐:

  1. 使用本机Windows API来创建线程。

  2. 使用一个好的库来管理它。 我通常使用一个名为JUCE的C ++框架来过上更好的生活。

使用Windows API,您可以尝试这样的事情:

/* * main.c * * Created on: 18/10/2011 * Author: Cesar Carlos Ortiz Pantoja. */ #include  #include  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; } 

问候