Linux system programming: line Hodohara language

Source: Internet
Author: User

Line Hodohara language

Threading Concepts

Thread, sometimes called a lightweight process (lightweight PROCESS,LWP), is the smallest unit of program execution flow. A standard thread consists of the thread ID, the current instruction pointer (PC), register collection, and Stack composition. More detailed explanation look Baidu Encyclopedia: Thread.

Under Linux shell, view all threads under the specified PID number by command $ PS-LF pid.


sharing and non-sharing between threads

The threads here refer to threads under the same process.

Shares:

1. File Descriptor Descriptor
2. How each signal is processed
3. Current working directory
4. User ID and Group ID
5. Memory address Space

Non-shared:

1. Thread ID
2. processor field and stack pointers (kernel stacks)
3. Independent stack space (user space stack)
4.errno variables
5. Signal Shielding Word
6. Scheduling priority


Line Hodohara Language

View all the thread-related functions under the system by command $ man-k pthread.

In general, the main function is referred to as the main thread or the master, and the other threads are called child threads.

Creating Threads

#include <pthread.h>int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*start_routine) ( void *), void *arg);

pthread_t *thread: Pass a pthread_t variable address in to save the new thread's TID (thread id)

Const pthread_attr_t *ATTR: Thread property settings, such as using default properties, are passed null

void * (*start_routine) (void *): function pointer, pointing to the new thread should load the executing function module

void *arg: Specifies the parameters of the function to which the thread will load the call

Return value: Successful return 0, Failure returns error number. Previously learned system functions are successful return 0, failed to return 1, and the error number is stored in the global variable errno, and the Pthread library function is returned by the return value of the error number, although each thread also has a errno, but this is to be compatible with other function interfaces, The Pthread library itself does not use it and returns the error code with a clearer return value.
Compile and Link with-lpthread.
typedef unsigned long int pthread_t; Pthread_t may have different implementations in different systems


End Thread

If you need to terminate only one thread without terminating the entire process, there are three ways to do this:

1.return. This method does not apply to the master thread, and return from the main function is equivalent to calling exit.

2. One thread can call Pthread_cancel to terminate another thread in the same process.

#include <pthread.h>int pthread_cancel (pthread_t thread);
The canceled thread, the exit value, defines the value of the constant pthread_canceled in the Linux pthread Library is-1. you can find its definition in the header file Pthread.h:#define PTHREAD_CANCELED ((void *)-1). That is, the thread terminated by Pthread_cancel, the exit value, is-1.

between threads in the same process, pthread_cancel terminates the signal to another line of Cheng. The system does not close the canceled thread immediately, and only the thread will actually end when the next system call is canceled. or call Pthread_testcancel to let the kernel detect if the current thread needs to be canceled.

3. Threads can call Pthread_exit to terminate themselves.

#include <pthread.h>void pthread_exit (void *retval), void *retval: A parameter passed out when a thread exits, can be an exit value or address, as an address, cannot be a local address that is requested internally by the thread.
       

Calling exit anywhere will cause the entire process to exit.


Thread ID
#include <pthread.h>pthread_t pthread_self (void); RETURN valuethis function always succeeds, returning the calling thread ' s ID.
use pthread_self in threads to get the thread ID.


Recycle thread
#include <pthread.h>int pthread_join (pthread_t thread, void **retval);p thread_t thread: The tidvoid **retval of the Recycle thread: The return value returned by the receiving exit Thread: Return 0 successfully, failure returns error number
similar to the process, when a thread exits, it also needs to be recycled, so pthread_join is similar to wait. The thread that called the function suspends the wait until a thread with the ID of thread terminates. Thread threads terminate in different ways, and the terminating state obtained through Pthread_join is different, summarized as follows:
    1. If the thread thread returns by return, retval points to a cell that contains the return value of the thread's function.
    2. If the thread thread is called by another thread, the pthread_cancel exception terminates, and retval points to the cell where the constant pthread_canceled is stored.
    3. If thread threads are terminated by their own invocation of Pthread_exit, retval points to the cell that holds the arguments passed to Pthread_exit.
If you are not interested in the terminating state of thread threads, you can pass NULL to the retval parameter.


code Exampleending a process with return and Pthread_exit
       

terminating another thread with Pthread_cancel
Thread_cancel.c#include <stdio.h> #include <pthread.h>void *fun (void *argv) {printf ("thread%x running\n ", Pthread_self ());p Thread_exit ((void*) 0);} int main (void) {pthread_t tid;void *ret;pthread_create (&tid, NULL, fun, NULL);p Thread_join (tid, &ret);p rintf (" Thread%x exit with%d\n ", Tid, (int.) ret);p thread_create (&tid, NULL, fun, NULL);p Thread_cancel (tid);p Thread_join ( Tid, &ret);p rintf ("Thread%x exit with%d\n", Tid, (int) ret); return 0;} $ gcc thread_cancel.c-lpthread$ a.outthread 96da9700 runningthread 96da9700 exit with 0thread 96da9700 Runningthread 96da 9700 Exit With-1


You can either use Pthread_cancel to terminate a child thread in the main thread, or you can terminate another child thread in a child thread.




CCPP Blog Directory


Copyright NOTICE: This article is for bloggers original articles, reproduced, please indicate the source.

Linux system programming: line Hodohara language

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.