Thread---created, terminated

Source: Internet
Author: User

Q:What is thread?

A: A running function----is running function----Multi-thread shared memory space

POSIX threads are a set of standards, not implementations

Thread ID: pthread_t type indeterminate: struct? or pointers? or integer number, think what is what, you can define it

[Email protected]:~$ PS AXM   
[Email protected]:~$ PS ax-l    //LWP

Process is the container's internal load thread

Function:

int pthread_equal (pthread_t t1, pthread_t T2);//Compare two threads to be the same; return non 0  otherwise 0
pthread_t pthread_self (void); Returns the current thread identity;

1. Create a thread:

int Const pthread_attr_t *attr,                    void * (*start_routine) (voidvoid *arg) ;

Parameter 1: Backfill thread representation

Parameter 2: Thread properties: Common null

Parameter 3: function pointer----sibling thread

Parameter 4: Parameters of the sibling thread

Return value: Successful return 0, failure directly returns error number

2. Termination of the thread:

3 ways: 1) The thread returns from the boot routine and the return value is the thread's exit code

2) threads can be canceled by other threads in the same process

3) Thread call Pthread_exit () function

void pthread_exit (void *retval); // Thread Termination
int void **retval); // thread corpse successfully returned 0, error returned wrong number  **retval only the corpse does not care about the state

3. Stack cleanup:

     //hook function       void Pthread_cleanup_push (void (*routine) (void *),void *arg);        void pthread_cleanup_pop (int  execute);

eg

#include <pthread.h>#include<string.h>Static void*func (void*p) {puts ("Thead is working!"); Pthread_exit (NULL);}intMain () {pthread_t tid; interr; Puts ("Begin!"); Err= Pthread_create (&tid,null,func,null); if(Err) {fprintf (stderr,"%s", Strerror (err)); Exit (1);    } pthread_join (Tid,null); Puts ("End!"); Exit (0);}
View Code

Ok! Rest and rest, continue the next section!

eg

#include <stdio.h>#include<stdlib.h>#include<pthread.h>#include<string.h>void*cleanup_func (void*p) {puts (P);}void*func (void*p) {puts ("Thead is working!"); Pthread_cleanup_push (Cleanup_func,"cleanup:1"); Pthread_cleanup_push (Cleanup_func,"Cleanup:2"); Pthread_cleanup_push (Cleanup_func,"Cleanup:3"); Pthread_cleanup_pop (1); Pthread_cleanup_pop (0); Pthread_cleanup_pop (1); Pthread_exit (NULL);}intMain () {pthread_t tid; interr; Puts ("Begin!"); Err= Pthread_create (&tid,null,func,null); if(Err) {fprintf (stderr,"%s", Strerror (err)); Exit (1);    } pthread_join (Tid,null); Exit (0);}
View Code

4. Thread cancellation

int pthread_cancel (pthread_t thread); //Cancel thread: Running line enters upgradeable cancel re-corpse

Thread---created, terminated

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.