Linux Threads-Basic applications

Source: Internet
Author: User

Feeling and hair (can directly ignore ~): Every time to use the thread, should be on the Internet to re-learn the foundation, the example is quite a lot: a good typesetting, not all the talk is not what they want; one is the typesetting is not good, directly skipped. Both have to struggle to find, so it is their own summary, I think every programmer has got a kind of look at others not pleasing to the disease, haha. I hope you can criticize correct, I this typesetting and summary have what to optimize, absolute best effort. This paper mainly introduces the basic application of Linux thread, and enumerates the usages and examples of several common functions. Header file pthread.h compile option requires adding-pthread common function thread to create function prototypes:
1 int Const void * (*start_routine) (voidvoid *arg);
Parameter description: Thread: Thread idattr: threading attribute, usually set to null (with other attributes, supplemented) Start_route (): Thread entry function arg: Thread entry function Parameter example:
1#include <stdio.h>2#include <unistd.h>3#include <pthread.h>4 5 voidThread_function (void*param)6 {7printf"This is a thread.\n");8 }9 intThread_test (void)Ten { One pthread_t thread_id; A     intret; -ret = Pthread_create (&thread_id, NULL, (void*) &thread_function, NULL); -     if(Ret! =0) { theprintf"pthread_create fail\n"); -         return-1; -     } -     +     /*The main purpose is to see the thread printing*/ -Sleep1); +         A } at intMainintargcChar*argv[]) - { - thread_test (); -     return 0; -}

Thread Exit Function prototype:

1 void pthread_exit (void *retval);
Note: The active exit for a thread is basically the same as the return function, but Pthread_cleanup_push () and Pthread_cleanup_pop () do not receive return value. To terminate another thread in the threads, you need to use Pthread_cancel (); parameter description: retval: The return value at the end of the thread, which can be obtained by other functions, such as pthread_join (). Wait for the thread function prototype:
1 int void **retval);
Description: The current thread will be suspended while waiting for the thread to exit. Parameter description: Thread: Waiting for the thread's Idretval: Example of the return value of the waiting thread:
1#include <stdio.h>2#include <unistd.h>3#include <pthread.h>4 pthread_t thread1_id, thread2_id;5 voidThread_function1 (void*param)6 {7printf"This is a thread 2.\n");8      while(1) {9printf"thread 1 is running.\n");TenSleep1); One     } Aprintf"Thread 1 exit.\n"); - } - voidThread_function2 (void*param) the { -printf"This is a thread 2.\n"); -Sleep2); -printf"thread 2 Cancel thread 1.\n"); + Pthread_cancel (thread1_id); - } + intThread_test (void) A { at     intret; -ret = Pthread_create (&thread1_id, NULL, (void*) &Thread_function1, NULL); -     if(Ret! =0) { -printf"pthread_create fail\n"); -         return-1; -     } inret = Pthread_create (&thread1_id, NULL, (void*) &Thread_function2, NULL); -     if(Ret! =0) { toprintf"pthread_create fail\n"); +         return-1; -     } theRET =Pthread_join (thread1_id, NULL); *     if(Ret! =0) { $printf"Pthread_join fail\n");Panax Notoginseng         return-1; -     } the } + intMainintargcChar*argv[]) A { the thread_test (); +     return 0; -}
To cancel a thread function prototype:
1 int pthread_cancel (pthread_t thread);
Description: Terminates another process in the thread. A thread can set the state to decide whether it can be canceled by another thread (pthread_setcancelstate), which can be canceled by default. Parameter description: Thread: The ID example of the thread to be canceled:
1#include <stdio.h>2#include <unistd.h>3#include <pthread.h>4 pthread_t thread1_id, thread2_id;5 voidThread_function1 (void*param)6 {7printf"This is a thread 2.\n");8      while(1) {9printf"thread 1 is running.\n");TenSleep1); One     } Aprintf"Thread 1 exit.\n"); - } - voidThread_function2 (void*param) the { -printf"This is a thread 2.\n"); -Sleep2); -printf"thread 2 Cancel thread 1.\n"); + Pthread_cancel (thread1_id); - } + intThread_test (void) A { at     intret; -ret = Pthread_create (&thread1_id, NULL, (void*) &Thread_function1, NULL); -     if(Ret! =0) { -printf"pthread_create fail\n"); -         return-1; -     } inret = Pthread_create (&thread1_id, NULL, (void*) &Thread_function2, NULL); -     if(Ret! =0) { toprintf"pthread_create fail\n"); +         return-1; -     } theRET =Pthread_join (thread1_id, NULL); *     if(Ret! =0) { $printf"Pthread_join fail\n");Panax Notoginseng         return-1; -     } the } + intMainintargcChar*argv[]) A { the thread_test (); +     return 0; -}

Just a brief introduction of the basic operation of the thread, more advanced applications later update please ~ ~

Linux Threads-Basic applications

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.