0% found this document useful (0 votes)
6 views4 pages

Howto-Guide: Getting Started With Posix Threads

POSIX standard library for creating and managing threads is called pthreads. This section describes how to set up your environment to be able to compile programs.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Howto-Guide: Getting Started With Posix Threads

POSIX standard library for creating and managing threads is called pthreads. This section describes how to set up your environment to be able to compile programs.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Institute for Program Structures and Data Organization (IPD)

Multicore Software Engineering Young Investigator Group

HowTo-Guide

Getting started with POSIX Threads

2007-10-24

Victor Pankratius (Editor) Wolfgang Schnerring

[Link]

University of Karlsruhe, Germany All rights reserved. The authors and editors have taken great care in the preparation of this report, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information contained therein.

2007-10-24 Wolfgang Schnerring <wosc@[Link]>

1 Getting started with POSIX threads


The POSIX standard library for creating and managing threads is called pthreads. This section describes how to set up your environment to be able to compile programs like the Pthreads-Hello world example shown in gure 1.
#include <pthread.h> #include <stdio.h>

void hello(void id) { printf("%d: Hello world!\n", ((int) id)); return 0; }

int main(int argc, char argv[]) { const int COUNT = 5; int i; pthread t thread[COUNT]; int ids[COUNT]; for (i = 0; i < COUNT; i++) { ids[i] = i; int retval = pthread create(&thread[i], NULL, hello, &ids[i]); if (retval) { perror("pthread_create failed"); return 1; } } for (i = 0; i < COUNT; i++) pthread join(thread[i], NULL); return 0; }

Figure 1: A hello-world program using POSIX threads.

1.1 Linux
libpthread is included in the glibc standard library, so no extra steps should be necessary (except for possibly installing the header les, which e. g. in Debian reside in package libc6dev). You should be able to compile programs using POSIX threads e. g. with
$ gcc o hello hellopthread.c lpthread

1.2 Windows
1.2.1 gcc Install GCC for Windows from the MinGW project [Link] you need the MinGW Runtime package.

1.2.2 pthreads Download the pthreads-win32 library from [Link] The easiest way is to get [Link] (with current version number v) which is a self-extracting archive. Unpack the archive and copy the les from the directory Prebuilt.2 into the MinGW-installation as follows: ($MINGW refers to the root directory of the installation, e. g. C:\mingw)
include/ $MINGW/include lib/.dll $MINGW/bin lib/ (the rest) $MINGW/lib

Include $MINGW/bin into your PATH environment variable. You should now be able to compile programs using POSIX threads, e. g. with
C:\>[Link] o hello hellopthread.c lpthread

You might also like