POSIX Threads API (pthreads) is a very common API used in parallel programming. This API includes many basic synchronization methods to facilitate the compilation of efficient multi-threaded programs. However, Microsoft Windows does not include such an interface. Fortunately, there is an open-source Windows platform for pthread implementation.
- Pthreads implementation on Microsoft Windows
- Open-source Protocol: BSD license
- Http://locklessinc.com/articles/pthreads_on_windows/
- : Http://locklessinc.com/downloads/
The downloaded package is just a winpthreads. h file, which can be used after being included in the project.
The following is an example of using this API. It is compiled in Visual Studio 2008/2010/2012 and runs correctly on Windows 7/Windows 8:
#include <stdio.h>#include <tchar.h>#include <winpthreads.h>/* This is our thread function. It is like main(), but for a thread */void *threadFunc(void *arg){char *str;int i = 0;str = (char*)arg;while(i < 10 ){Sleep(1);printf("threadFunc says: %s\n",str);++i;}return NULL;}int _tmain(int argc, _TCHAR* argv[]){pthread_t pth;// this is our thread identifierint i = 0;/* Create worker thread */pthread_create(&pth,NULL,threadFunc,"processing...");/* wait for our thread to finish before continuing */pthread_join(pth, NULL /* void ** return value could go here */);while(i < 10 ){Sleep(1);printf("main() is running...\n");++i;}return 0;}
Running result:
References:
- Pthreads on Microsoft http://locklessinc.com/articles/pthreads_on_windows/
- POSIX Threads http://en.wikipedia.org/wiki/POSIX_Threads
- Multithreading in C, POSIX style http://softpixel.com /~ Cwright/programming/Threads. C. php
- Pthreads-win32 http://sourceware.org/pthreads-win32/