Use the pthread library in Windows (can be used by GCC Cl compilers)

Source: Internet
Author: User

Nothing is unexpected.

Porting from Linux to Windows C ++ProgramThe socket and thread library must be transformed.

One day, I suddenly thought that windows could use the pthread library? There is a thread solution for searching.

1. Download: http://sourceware.org/pthreads-win32/#download

Download the latest one directly. For now: prebuilt-dll-2-9-1-release.zip because it is a Windows platform, so download. Zip or. For example, if exeis enabled, .exe is a self-Extracting tool. The latest version does not have the download version of .exe. The content is the same as that of zip. But zip and tar.gz tar.bz2 are not the same. Zip is more comprehensive, including the compiled binary Pre-built.2, source code pthreads.2, there is a 3. queueuserapcex does not know what to do. It is an alert driver and DDK is required for compiling. It is not installed by default in vs2010. Install the Windows Device Driver kit ntddk. h separately.

2. install these development libraries in your environment, including lib to the Lib folder of Vs, DLL to the path directory, and include to the include folder of. Is this more convenient.

It is easy to write two scripts:

 Rem  Put this file to: D :\# win pthread \ pthreads-w32-2-9-1-release \ Pre-built.2  Copy Include \ pthread. H" C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ Vc \ include \ "pthread. hcopy include \ sched. h "C: \ Program Files (X86) \ Microsoft Visual Studio 10.0 \ Vc \ include \" Sched. hcopy include \ semaphore. h "C: \ Program Files (X86) \ Microsoft Visual Studio 10.0 \ Vc \ include \" Semaphore. hcopy dll \ x86 \ pthreadvc2.dll % WinDir % \ pthreadvc2.dllcopy dll \ x86 \ pthreadvce2.dll % WinDir % \ export dll \ x86 \ pthreadvse2.dll % WinDir % \ export Lib \ x86 \ pthreadvc2.lib "C: \ Program Files (X86) \ Microsoft Visual Studio 10.0 \ Vc \ Lib \"Pthreadvc2.lib copy Lib \ x86 \ pthreadvce2.lib "C: \ Program Files (X86) \ Microsoft Visual Studio 10.0 \ Vc \ Lib \" Pthreadvce2.lib copy Lib \ x86 \ pthreadvse2.lib "C: \ Program Files (X86) \ Microsoft Visual Studio 10.0 \ Vc \ Lib \" Pthreadvse2.lib Ping-N 2 127.1> NUL 
 Rem  Put this file to: D :\# win pthread \ pthreads-w32-2-9-1-release \ Pre-built.2  Del /F/A/Q" C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ Vc \ include \ "pthread. hdel/f/A/Q "C: \ ProgramFiles (X86) \ Microsoft Visual Studio 10.0 \ Vc \ include \" Sched. hdel/f/A/Q "C: \ Program Files (X86) \ Microsoft Visual Studio 10.0 \ Vc \ include \" Semaphore. hdel/f/A/Q % WinDir % \ pthreadvc2.dlldel/f/A/Q % WinDir % \ pthreadvce2.dlldel/f/A/Q % WinDir % \ pthreadvse2.dlldel/f/A/Q "C: \ Program Files (X86) \ Microsoft Visual Studio 10.0 \ Vc \ Lib \" Pthreadvc2.lib del/f/A/Q "C: \ Program Files (X86) \ Microsoft Visual Studio 10.0 \ Vc \ Lib \" Pthreadvce2.lib del/f/A/Q "C: \ ProgramFiles (X86) \ Microsoft Visual Studio 10.0 \ Vc \ Lib \" Pthreadvse2.lib Ping-N 2 127.1> NUL 

The preceding paths may be different. Correct them by yourself.

3. Test the pthread library in windows.

# Include <stdio. h> # Include <Pthread. h> # Include <Windows. h> # Pragma Comment (Lib, "pthreadvc2.lib ") //  This sentence must be added.   Void * Function_t ( Void * Param) {pthread_t myid =Pthread_self ();  While ( 1  ) {Printf (  "  Thread id = % d \ n  "  , Myid); sleep (  4000  );}  Return  NULL ;}  Int  Main () {pthread_t PID; pthread_create ( & PID, null, function_t, null ); While ( 1  ) {Printf (  "  In fatherprocess! \ N  "  ); Sleep (  2000  );} Getchar ();  Return   1  ;} 

This is a copy of the user, the example is not particularly good, just use pthread_create.

Including the windows. h and pthread. h header files.

Because it uses the DLL method to link the pthread library, the DLL needs to go to the path and put it in the executable file directory. As to whether the file can be directly statically linked to the executable file, I think it should be very easy to compile the Windows pthread library by myself.

I also wrote a batch for compiling this test file. Now I can see the batch processing directly, so I can understand everything.

 Rem  0. ========= initialize the vs Environment  Call "C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ Vc \ vcvarsall. Bat" X86  Rem  1. ==========  Rem  Add # pragma comment (Lib, "pthreadvc2.lib") to the source file ") Cl.exe testwpthread. CPP  Rem  2. ==========  Rem Cl.exe testwpthread. CPP/C/I "C: \ pthreads-w32-2-7-0-release \ Pre-built.2 \ include"  Rem  Link.exe testwpthread. OBJ/libpath: "C: \ pthreads-w32-2-7-0-release \ Pre-built.2 \ Lib" pthreadvc2.lib  Testwpthread . EXE  Del /F *. EXE  Del /F *. OBJ  CMD 

Link to the Lib library. Under the cl.exe compiler, you can use # pragma comment (Lib, "pthreadvc2.lib") or add it to the cl.exe parameter.

Compile the Windows pthread library and change it to a static Link Library.

 

Reference link:

Http://blog.csdn.net/michaelsrc/article/details/6665493

Http://ah-fu.iteye.com/blog/281644

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.