The Open Source Swiss Army Knife

/code/c/unix_c/threads/
/code/c/unix_c/threads/ + sub-categories
http://www.sirfsup.com/
web directory content
    
      

Not logged in
Chat Register Login
return to:  http:/www.sirfsup.com      /code   /c   /unix_c   /threads 
Permalink: threads.htm
Title: add
article options : please login   |  print view

threads in C

  1. thread theory
  2. rationale and application
  3. reentrancy and issues raised by their use
  4. compiling
  5. and operating systems
  6. pthread_join: order
  7. functions
  8. cancellation

rationale and application

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:

  1. program counter
  2. stack
  3. scheduling parameters
" 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

reentrancy

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:

  • mutexes are needed to protect the data structres shared from the main program loop;
  • mutexes are a thread object used to solve concurrency issues by itself

compiling

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)

and operating systems

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: order

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.

functions

  • the function must accept only a single void * parameter
  • the function must return only a single void * parameter
  • pthread_create: creates the thread; see processfd_test.c to see how to call that "function" as a thread by the use of pthread_create
    note that there is no "run" method, but that function is automatically called
    last parameter is a pointer to an int, which is the only value allowed to pass to the function which is the thread
  • pthread_join: like wait after a fork.

cancellation

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
Your Name:     anonymous
Your Email:
Website:  
Comments:

The author will be notified of your reply.
return to top