Pthreads on Microsoft Windows

Source: Internet
Author: User

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/

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.