Share on Facebook Share on Twitter Email
Answers.com

POSIX Threads

 
Wikipedia: POSIX Threads

POSIX Threads, or Pthreads, is a POSIX standard for threads. The standard defines an API for creating and manipulating threads. Pthreads are most commonly used on Unix-like POSIX systems such as FreeBSD, NetBSD, GNU/Linux, Mac OS X and Solaris, but Microsoft Windows implementations also exist. For example, the pthreads-w32 is available and supports a subset of the Pthread API for the Windows 32-bit platform.[1]

Contents

Contents

Pthreads defines a set of C programming language types, functions and constants. It is implemented with a pthread.h header and a thread library. Programmers can use Pthreads to create, manipulate and manage threads, as well as synchronize between threads using mutexes, condition variables and semaphores.

Example

An example of using Pthreads in C:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
 
static void *thread_func(void *vptr_args)
{
    int i;
 
    for (i = 0; i < 20; i++)
    {
        fputs("  b\n", stderr);
        sleep(1);  // suspend thread for one second.
    }
 
    return NULL;
}
 
int main(void)
{
    int i;
    pthread_t thread;
 
    if (pthread_create(&thread, NULL, thread_func, NULL) != 0)
    {
        return EXIT_FAILURE;
    }
 
    for (i = 0; i < 20; i++)
    {
        fputs("a\n", stdout);
        sleep(1);
    }
 
    if (pthread_join(thread, NULL) != 0)
    {
        return EXIT_FAILURE;
    }
 
    return EXIT_SUCCESS;
}

This program creates a new thread that prints lines containing 'b', while the main thread prints lines containing 'a'. The output is interleaved between 'a' and 'b' as a result of execution switching between the two threads, or simultaneous execution on a multicore system. In any case, the pattern of 'a's and 'b's does not strictly alternate between each letter, and can vary even between different runs on the same machine.

An example of using Pthreads in C++:

// you can compile it with gcc using 'g++ -pthread -o test example.cpp'
#include <cstdlib>
#include <iostream>
#include <memory>
 
#include <pthread.h>
 
class Thread
{
private:
    pthread_t thread;
 
    Thread(const Thread& copy);         // copy constructor denied
    static void *thread_func(void *d)   { ((Thread *)d)->run(); return 0; }
 
public:
    Thread()             {}
    virtual ~Thread()    {}
 
    virtual void run() = 0;
 
    int start()          { return pthread_create(&thread, NULL, Thread::thread_func, (void*)this); }
    int wait ()          { return pthread_join  (thread, NULL); }
};
 
typedef std::auto_ptr<Thread> ThreadPtr;
 
int main(void)
{
    class Thread_a:public Thread
    {
    public:
        void run()
        {
            for (int i=0; i<20; i++, sleep(1))
                std::cout << "a  " << std::endl << std::flush;
        }
    };
 
    class Thread_b:public Thread
    {
    public:
	void run()
        {
            for(int i=0; i<20; i++, sleep(1))
                std::cout << "  b" << std::endl << std::flush;
        }
    };
 
    ThreadPtr a( new Thread_a() );
    ThreadPtr b( new Thread_b() );
 
    if (a->start() != 0 || b->start() != 0)
        return EXIT_FAILURE;
 
    if (a->wait() != 0 || b->wait() != 0)
        return EXIT_FAILURE;
 
    return EXIT_SUCCESS;
}

This program creates two new threads, one which prints lines containing 'a', and one which prints lines containing 'b', while the main thread exits without printing anything. Like in the example above, the output is interleaved between 'a' and 'b' as a result of execution switching between the two threads, or simultaneous execution on a multicore system.

See also

References

Further reading

  • David R. Butenhof: Programming with POSIX Threads, Addison-Wesley, ISBN 0-201-63392-2
  • Bradford Nichols, Dick Buttlar, Jacqueline Proulx Farell: Pthreads Programming, O'Reilly & Associates, ISBN 1-56592-115-1
  • Charles J. Northrup: Programming with UNIX Threads, John Wiley & Sons, ISBN 0-471-13751-0
  • Kay A. Robbins and Steven Robbins, UNIX Systems Programming, Prentice-Hall, ISBN 0-13-042411-0

External links


Search unanswered questions...
Enter a question here...
Search: All sources Community Q&A Reference topics
 
 

 

Copyrights:

Wikipedia. This article is licensed under the Creative Commons Attribution/Share-Alike License. It uses material from the Wikipedia article "POSIX Threads" Read more