免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 981 | 回复: 0
打印 上一主题 下一主题

POSIX thread (pthread) libraries (ZZ) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-11 10:51 |只看该作者 |倒序浏览

  
   
      
      

      
      
      POSIX thread (pthread) libraries
      
The POSIX thread libraries are a standards based thread API for C/C++.
It allows one to spawn a new concurrent process flow. It is most effective
on multi-processor or multi-core systems where the process flow can be scheduled to run on
another processor thus gaining speed through parallel or distributed processing.
Threads require less overhead than "forking" or spawning a new process because
the system does not initialize a new system virtual memory space and environment for
the process. While most effective on a multiprocessor system, gains are
also found on uniprocessor systems which exploit latency in I/O and other
system functions which may halt process execution. (One thread may execute
while another is waiting for I/O or some other system latency.)
Parallel programming technologies such as MPI and PVM are used in a distributed
computing environment while threads are limited to a single computer system.
All threads within a process share the same address space.
A thread is spawned by defining a function and it's arguments which will
be processed in the thread.
The purpose of using the POSIX thread library in your software is
to execute  software faster.
      
      
   
  
  
   
      
      Table of Contents:
      
      
      
      
      
      
      
      
   
  
  
   
      
      
      
      Related YoLinux Tutorials:
      
      
      °
C++ on Linux
      
      
      °
C++ STL (Standard Template Library) example of a linked list using a list
      
      
      °
C++ string class examples
      
      
      °
X-emacs and C++ development
      
      
      °
C++ Structure Example and Tutorial
      
      
      °
Linux software development tutorial
      
      
      °
YoLinux Tutorials Index
      
      
      
      
      
      

      
      
      
      
      
      
      
      
      
      
      
      

Free Information Technology Magazine Subscriptions and Document Downloads

      
      
      
      
      
Free Information Technology Software and Development Magazine Subscriptions and Document Downloads
      
      
      
      
      
      
      
      
      
        
         
            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
      
      
         
            
              
               
                #include
#include
#include
void *print_message_function( void *ptr );
main()
{
     pthread_t thread1, thread2;
     char *message1 = "Thread 1";
     char *message2 = "Thread 2";
     int  iret1, iret2;
    /* Create independent threads each of which will execute function */
     iret1 =
pthread_create
( &thread1, NULL, print_message_function, (void*) message1);
     iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
     /* Wait till threads are complete before main continues. Unless we  */
     /* wait we run the risk of executing an exit which will terminate   */
     /* the process and all threads before the threads have completed.   */
     
pthread_join
( thread1, NULL);
     pthread_join( thread2, NULL);
     printf("Thread 1 returns: %d\n",iret1);
     printf("Thread 2 returns: %d\n",iret2);
     exit(0);
}
void *print_message_function( void *ptr )
{
     char *message;
     message = (char *) ptr;
     printf("%s \n", message);
}
               
              
            
         
        
      
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

       
             
       
                  
                     
       
                        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.
         

             
             
            
  • Function call:
    pthread_exit

       
                  void pthread_exit(void *retval);
       
        Arguments:
       
             
    • retval - Return value of thread.
         

       
             
        This routine kills the thread. The pthread_exit function never
        returns. If the thread is not detached, the thread id and return value
        may be examined from another thread by using pthread_join.
       
        Note: the return pointer *retval, must not be of local scope
        otherwise it would cease to exist once the thread terminates.
       
             
       
            
  • [color="#ff0000"][C++ pitfalls]: The above sample program
            will compile with the GNU C and C++ compiler g++.
            The following function pointer representation below
            will work for C but not C++. Note the subtle differences and avoid the
            pitfall below:
       
                  void print_message_function( void *ptr );
        ...
        ...
        iret1 = pthread_create( &thread1, NULL, (void*)&print_message_function, (void*) message1);
        ...
        ...
       
            

      
      
      
      
      
        
         
            Thread Synchronization:
         
        
      
      
The threads library provides three synchronization mechanisms:
      
      
  • mutexes - Mutual exclusion lock: Block access to variables
    by other threads. This enforces exclusive access by a thread to a
    variable or set of variables.
  • joins - Make a thread wait till others are complete (terminated).
            
  • condition variables - data type pthread_cond_t
            

      
      
      
      Mutexes:
Mutexes are used to prevent data inconsistencies due to race conditions.
A race condition often occurs when two or more threads need to perform
operations on the same memory area, but the results of computations depends on
the order in which these operations are performed. Mutexes are used for
serializing shared resources.  Anytime a global resource is accessed by more
than one thread the resource should have a Mutex associated with it.
One can apply a mutex to protect a segment of memory ("critical region") from other threads.
Mutexes can be applied only to threads in a single process and do not work
between processes as do semaphores.
      
Example threaded function:
      
      
         
            
              
                Without Mutex
                With Mutex
              
              
               
                int counter=0;
/* Function C */
void functionC()
{
   counter++
}
               
               
               
                /* Note scope of variable and mutex are the same */
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int counter=0;
/* Function C */
void functionC()
{
   pthread_mutex_lock( &mutex1 );
   counter++
   pthread_mutex_unlock( &mutex1 );
}
               
              
              
                Possible execution sequence
              
              
                Thread 1
                Thread 2
                Thread 1
                Thread 2
              
              
               
counter = 0
               
               
counter = 0
               
               
counter = 0
               
               
counter = 0
               
              
              
               
counter = 1
               
               
counter = 1
               
               
counter = 1
               
               
Thread 2 locked out.
Thread 1 has exclusive use of variable counter
               
              
              
               
               
               
               
               
               
               
               
               
               
counter = 2
               
              
            
         
        
      
If register load and store operations for the incrementing of variable counter
occurs with unfortunate timing, it is theoretically possible to have each
thread increment and overwrite the same variable with the same value.
Another possibility is that thread two would first increment counter
locking out thread one until complete and then thread one would increment it to 2.
      
      
      
         
            
              
                Sequence
                Thread 1
                Thread 2
              
              
                1
                counter = 0
                counter=0
              
              
                2
                Thread 1 locked out.
Thread 2 has exclusive use of variable counter
               
                counter = 1
              
              
                3
                counter = 2
               
               
              
            
         
        
      
Code listing: mutex1.c
      
      
         
            
              
               
                #include
#include
#include
void *functionC();
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int  counter = 0;
main()
{
   int rc1, rc2;
   pthread_t thread1, thread2;
   /* Create independent threads each of which will execute functionC */
   if( (rc1=pthread_create( &thread1, NULL, &functionC, NULL)) )
   {
      printf("Thread creation failed: %d\n", rc1);
   }
   if( (rc2=pthread_create( &thread2, NULL, &functionC, NULL)) )
   {
      printf("Thread creation failed: %d\n", rc2);
   }
   /* Wait till threads are complete before main continues. Unless we  */
   /* wait we run the risk of executing an exit which will terminate   */
   /* the process and all threads before the threads have completed.   */
   pthread_join( thread1, NULL);
   pthread_join( thread2, NULL);
   exit(0);
}
void *functionC()
{
   
pthread_mutex_lock
( &mutex1 );
   counter++;
   printf("Counter value: %d\n",counter);
   
pthread_mutex_unlock
( &mutex1 );
}
               
              
            
         
        
      
Compile: cc -lpthread mutex1.c
      
Run: ./a.out
      
Results:
      
      
         
          Counter value: 1
Counter value: 2
         
        
      
When a mutex lock is attempted against a mutex which is held by another thread,
the thread is blocked until the mutex is unlocked.
When a thread terminates, the mutex does not unless explicitly unlocked.
Nothing happens by default.
      
      
      
      
      Joins:
A join is performed when one wants to wait for a thread to finish. A thread
calling routine may launch multiple threads then wait for them to finish
to get the results. One wait for the completion of the threads with a join.
      
Sample code: join1.c
      
      
         
            
              %0

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/22326/showart_493350.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP