Linux Tutorial: POSIX Threads

Linux Tutorial: POSIX Threads

Thread Basics:
  • Thread operations include thread creation, termination, synchronization (joins,blocking), scheduling, data management and process interaction.
  • A thread does not maintain a list of created threads, nor does it know the thread that created it.
  • All threads within a process share the same address space.
  • Threads in the same process share:
    • Process instructions
    • Most data
    • open files (descriptors)
    • signals and signal handlers
    • current working directory
    • User and group id
  • Each thread has a unique:
    • Thread ID
    • set of registers, stack pointer
    • stack for local variables, return addresses
    • signal mask
    • priority
    • Return value: errno
  • pthread functions return "0" if OK.

Thread Creation and Termination:

Example: pthread1.c

01#include <stdio.h>
02#include <stdlib.h>
03#include <pthread.h>
04 
05void *print_message_function( void *ptr );
06 
07main()
08{
09     pthread_t thread1, thread2;
10     char *message1 = "Thread 1";
11     char *message2 = "Thread 2";
12     int  iret1, iret2;
13 
14    /* Create independent threads each of which will execute function */
15 
16     iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
17     iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
18 
19     /* Wait till threads are complete before main continues. Unless we  */
20     /* wait we run the risk of executing an exit which will terminate   */
21     /* the process and all threads before the threads have completed.   */
22 
23     pthread_join( thread1, NULL);
24     pthread_join( thread2, NULL);
25 
26     printf("Thread 1 returns: %d\n",iret1);
27     printf("Thread 2 returns: %d\n",iret2);
28     exit(0);
29}
30 
31void *print_message_function( void *ptr )
32{
33     char *message;
34     message = (char *) ptr;
35     printf("%s \n", message);
36}

Compile:

  • C compiler: cc -lpthread pthread1.c
    or
  • C++ compiler: g++ -lpthread pthread1.c

Run: ./a.out
Results:
Thread 1
Thread 2
Thread 1 returns: 0
Thread 2 returns: 0

Details:

  • In this example the same function is used in each thread. The arguments are different. The functions need not be the same.
  • Threads terminate by explicitly calling pthread_exit, by letting the function return, or by a call to the function exit which will terminate the process including any threads.
  • Function call: pthread_create - create a new thread
        int pthread_create(pthread_t * thread, 
                           const pthread_attr_t * attr,
                           void * (*start_routine)(void *), 
                           void *arg);
    
    Arguments:
    • thread - returns the thread id. (unsigned long int defined in bits/pthreadtypes.h)
    • attr - Set to NULL if default thread attributes are used. (else define members of the struct pthread_attr_t defined in bits/pthreadtypes.h) Attributes include:
      • detached state (joinable? Default: PTHREAD_CREATE_JOINABLE. Other option: PTHREAD_CREATE_DETACHED)
      • scheduling policy (real-time? PTHREAD_INHERIT_SCHED,PTHREAD_EXPLICIT_SCHED,SCHED_OTHER)
      • scheduling parameter
      • inheritsched attribute (Default: PTHREAD_EXPLICIT_SCHED Inherit from parent thread: PTHREAD_INHERIT_SCHED)
      • scope (Kernel threads: PTHREAD_SCOPE_SYSTEM User threads: PTHREAD_SCOPE_PROCESS Pick one or the other not both.)
      • guard size
      • stack address (See unistd.h and bits/posix_opt.h _POSIX_THREAD_ATTR_STACKADDR)
      • stack size (default minimum PTHREAD_STACK_SIZE set in pthread.h),
    • void * (*start_routine) - pointer to the function to be threaded. Function has a single argument: pointer to void.
    • *arg - pointer to argument of function. To pass multiple arguments, send a pointer to a structure
原文地址:https://www.cnblogs.com/lexus/p/2887115.html