/>
threads
 jbwyatt.com

.. threads

Thread = lightweight process
LESS system resources
LESS overhead

NO copy of memory space used

Threads of execution are simply differet execution sequences through a common set of code.

Multithreading allows multiple threads to exist within the context of a single process, sharing the process' resources but able to execute independently. If a program contains a number of procedures and all of these procedures could be scheduled to run simultaneously and/or independently by the operating system, that would be like a threaded model of execution. The threaded programming model provides a useful abstraction of concurrent execution and allows a single process to be executed in PARALLEL on a multiprocessor system. Threaded processes must be thread safe

.. processes vs. threads

Processes contain info about program resources and program execution state:
    * Process ID, process group ID, user ID, and group ID
    * Environment
    * Working directory.
    * Program instructions
    * Registers
    * Stack
    * Heap
    * File descriptors
    * Signal actions
    * Shared libraries
    * IPC tools (message queues, pipes, semaphores, or shared memory).    

 
This independent flow of control occurs because a thread maintains its own: * Stack pointer * Registers * Scheduling properties (such as policy or priority) * Set of pending and blocked signals * Thread specific data. So in the UNIX environment a thread: * Exists within a process and uses the process resources * Has its own independent flow of control * Duplicates only the essential resources it needs * May share the process resources with other threads * Dies if the parent process dies * "lightweight" because most of the overhead has already been accomplished through the creation of its process. Because threads within the same process share resources: * Changes made by one thread to shared system resources (such as closing a file) will be seen by all other threads. * Two pointers having the same value point to the same data. * Reading and writing to the same memory locations is possible, and therefore requires explicit synchronization by the programmer. from https://computing.llnl.gov/tutorials/pthreads/

.. code: creating threads


pthread_create - thread creation pthread_create creates a new thread and makes it executable. This routine can be called any number of times from anywhere within your code. pthread_create arguments: * thread: An opaque, unique id for the new thread returned by the subroutine. * attr: An opaque attribute object that may be used to set thread attributes. Can specify a thread attributes object, or NULL for default values. * start_routine: C routine that the thread will execute once it is created. * arg: A single argument that may be passed to start_routine. It must be passed by reference as a pointer cast of type void. NULL may be used if no argument is to be passed. Once created, threads are peers, and may create other threads. There is no implied hierarchy or dependency between threads.
#include <pthread.h> #include <stdio.h> #define NUM_THREADS 5 /* thread code that each thread executes void *PrintHello(void *threadid) { int tid; tid = (int)threadid; printf("Hello World! It's me, thread #%d!\n", tid); pthread_exit(NULL); } int main (int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc, t; for(t=0; t<NUM_THREADS; t++){ printf("In main: creating thread %d\n", t); rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); if (rc) { printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } } pthread_exit(NULL); }
POSIX or "Portable Operating System Interface" is the collective name of a family of related standards specified by the IEEE to define the application programming interface (API), along with shell and utilities interfaces for software compatible with variants of the Unix operating system, although the standard can apply to any operating system. - attempt to standardize - requires cc switch jupiter|/export/home/wyatt/private/355$ cc threads.c -lthread -lpthread ^^^^^^^^^ jupiter|/export/home/wyatt/private/355$ a.out In main: creating thread 0 In main: creating thread 1 In main: creating thread 2 In main: creating thread 3 In main: creating thread 4 Hello World! It's me, thread #0! Hello World! It's me, thread #1! Hello World! It's me, thread #2! Hello World! It's me, thread #3! Hello World! It's me, thread #4!