UNIX Environment Advanced Programming: Threading

Source: Internet
Author: User
Tags thread

Threads contain the information necessary to represent the execution environment within a process, including the thread ID of the thread in the process, a set of register values, stacks, scheduling precedence and policies, signal masks, errno variables, and thread private data.

All information about the process is shared with all threads of the process, including executable program text, the program's global memory and heap memory, stacks, and file descriptors.

Thread Identification:

The process ID is unique across the system, but with a different thread ID, the thread ID is only valid in the process environment in which it belongs. The data structure of the process ID is pid_t, and the data structure of the thread ID is pthread_t.

Compare two thread IDs for equality:

#include <pthread.h>  
int pthread_equal (pthread_t tid1,pthread_t tid2);/return value: If equal returns a value not 0, return 0

A thread can get its own thread ID by calling the Pthread_self function:

#include <pthread.h>  
pthread_t pthread_self (void);//return value: Thread ID of the calling thread

Creation of Threads:

#include <pthread.h>  
int pthread_create (pthread_t *tidp,const pthread_attr_t *attr,void * (*START_RTN) (void *), void * arg)//return value: Returns 0 if successful, or returns an error number

When Pthread_create returns successfully, the memory unit pointed to by TIDP is set to the thread ID of the newly created thread. The attr parameter is used to customize a variety of thread properties. If set to NULL, represents the thread that created the default property.

The newly created thread runs from the address of the START_RTN function, which has only one arg of the parameterless pointer parameter, and if more than one argument is required to pass to the START_RTN function, the arguments need to be placed in a struct and the address of the struct is passed in as an arg parameter.

When a thread is created, it does not guarantee which thread will run first: whether it is a newly created thread or a calling thread. The newly created thread can access the process address space and inherit the floating-point environment and signal mask Word of the calling thread, but the thread's pending signal set is cleared.

Each thread provides a copy of the errno.

Note: Pthread_create is not returned until the START_RTN function is completed. Normally the Pthread_create function returns first, but sometimes the START_RTN function is completed before Pthread_create returns. There is no order between them.

Sample code:

#include <stdio.h>  
#include <stdlib.h>  
#include <pthread.h>  
#include <unistd.h >  
#include <sys/types.h>  
#include <unistd.h>  
      
pthread_t ntid;  
      
void Printids (const char *s)  
{  
    pid_t pid;  
    pthread_t Tid;  
    PID = Getpid ();  
    Tid = Pthread_self ();  
    printf ("%s pid =%u pthread_t =%u\n", s, (unsigned int) PID, (unsigned int) tid);  
      
void* thr_fn (void * arg)  
{  
    printids ("New pthread:");  
    return NULL;  
}  
      
int main (int argc,char **argv)  
{  
    int err;  
    Err = Pthread_create (&ntid,null,thr_fn,null);  
    if (Err!= 0)  
      perror ("Pthread_create");  
    Printids ("Main thread:");  
    Sleep (1);  
    Exit (0);  
}

Run Result:

huangcheng@ubuntu:~$./a.out
 new Pthread ID = 3077581680  
main thread:  pid = 2458 pthread_t = 3077584576
  new pthread:  pid = 2458 pthread_t = 3077581680

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.