Tags: %TAGME{ tpaction="" web="SCMP391" tag="" }% view all tags

POSIX Threads Programming

POSIX Threads Programming, or pthreads, allow you program to run multiple components concurently. This is espesially useful if you are running a blocking command (like cin or reading from a fifo), and still want the programming to be able to do something while it waits.

Very good full documentation is here: POSIX Threads Programming

There are two demo programs programs on the server.

1. Simple multithreading

In: /home/class/SoftDev/ThreadExample

Is an example of a program that create 5 threads.

2. Multithreading with timers and thread cancelling

In: /home/class/SoftDev/ThreadExampleTime

Sometimes you start a blocking operation (like a cin or a fifo open or read), and it gets stuck. What you need is a way to have a timeout that stops the blocked thread after a certain amount of time (called a timeout).

What you can do is create two threads. In one thread you do the blocking operation. In the other you start a thread and make it sleep for a given time period (the timeout). If the blocking operation ends first, it cancels the timer thread. If the timer wakes up first, you have waited enough, and you timer thread cancels the blocked thread. It can then do whatever make sense next.

Edit | Attach | Watch | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r2 - 2017-04-08 - JimSkon
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback