|
|||||
| | |||||
use 1: monitoring multiple file descriptors. (the problem with forking is interprocess communication, start-up time.)
the POSIX threads are called POSIX:THR thread extensions
Threads can take advantage of parallelism
They are blocking but that is not readily visible if the thread contains no timing information
threads have their own:
" Threads share a common address space, and are often scheduled internally in a process, thereby avoiding a lot of the inefficiencies of multiple processes."source: here
reentrant means (for relocatable code) inherently relocatable. It is referring to I guess in the threads context that different instances can refer to unique addresses. Unlike strtok.
reentrant functions don't have statics or shared data. The nonrenetrant functions can be made reentrant (and thread-safe) by the use of mutexes, but that also makes them slower than their recoded variations.
Examples of non-reentrant functions are ctime (returns a pointer to static data that is overwritten by each call) and strtok. And malloc and free (whew! that gets rid of a lot of functions, non?) The things which are up-for-grabs are errno, gethostbyname,getpwuid. You can use instead strtok_r or ctime_r. For example of testing these functions see pgsql_threads sample souce from a pg distribution.
pid/group id/environment/cwd are not shared between threads
signals/file descriptors/shared libararies/ipc are shared between threads
In sum, never use global or static variables inside your functions. Never call non-reentrant functions inside your function. If you absolutely need a global variable lock it with a mutex. Never return a pointer to static data. The best way is to make the caller provides the storage for the data you need to return. But you have to modify the interface to your non-reentrant functions.
source: link
other steps are detailed at http://nscp.upenn.edu/aix4.3html/aixprggd/genprogc/writing_reentrant_thread_safe_code.htm:
Identifying exported global variables. Those variables are usually defined in a header file with the export keyword.
Exported global variables should be encapsulated. The variable should be made private (defined with the static keyword in the library source code), and access (read and write) subroutines should be created.
To make concurrency issues go away, and provide reentrancy to those which are not:
Need to include <pthread.h> and link against libpthread.so
to test these samples, use select_term.c which produces multiple file descriptors for monitoring (uses 'socket' and was contrived from example which did not)
source = Oracle8i for Windows NT Starter Kit, by Steve Bobrowski, ISBN 0-07-212248-X.
On winNT each Oracle database instance executes as a single process, and the threads execute in the background. In this case as well as the unix case the threads are performingn (each) a specialized function.
user and kernel threads can be implemented in different ways: M:1 (all threads mapped to one kernel thread), 1:1, M:N (all user threads mapped to a pool of kernel threads)
pthread_join
which thread runs first is not settable by us, it depends.
So a thread can be spawned, but never run, if the main thread runs first and calls exit before that thread has the chance to run.
This thread is about thread_join() function. If the main process executes first it will wait and give the thread a chnace to execute before the main thread exits before the main thread can do its work.
Program correctness depends on writing the correct program.
threads control their own cancellation, but any thread may request the cancellation of another thread.
| (#43) poster : anonymous (owner) | date: 2006-02-19 |
| look here | |
| Leave a Reply |