Linux C Programming Multithreading basic article

Source: Internet
Author: User

Linux C Programming Multithreading basic article

Preface: Because of the design and implementation of the Linux network intrusion detection system want to use multi-threading, so want to learn the Linux C programming multithreading knowledge

Note: Because the Pthraed library is not the default Linux system library, so in the multi-threaded development, need to add the header file # # <pthread.h>, compile with parameters-lpthread; GCC thread.c-o Thread-lpthread.

Processes and Threads:

    1. Process is the basic unit of program execution, resource allocation, each process has its own data segment, code snippet, stack segment, there is a more complex context switch during process switching

    2. A thread is a process-independent running route, which is the smallest unit of processor scheduling, also known as a lightweight process, that can access the memory space of the process, and share memory space, code, and resources with other threads of the process.

    3. Thread-related execution state and storage variables are placed in the inline control table, a process can have multiple threads, multiple thread control tables and stack registers, and a user address space is shared

650) this.width=650; "src=" Http://pic002.cnblogs.com/images/2012/350751/2012040513175742.jpg "alt=" 2012040513175742.jpg "/>

Important noun:

Thread identity: The thread ID, the process ID is unique throughout the system, the thread ID is valid only in the environment of the owning process, the thread identity is represented by the data structure pthread_t, the pthread_t cannot be processed as an integer, and Linux uses unsigned long-shaped numbers to represent pthread_t.

Important functions:

    1. Pthread_self ()

Function prototype: pthread_t phtread_self (void)

Parameters: None

return value: (in progress) thread ID of the calling thread

2. Pthread_equal ()

Function prototype: int pthread_equal (pthread_t tidone,pthread_t tidtwo)

Parameters: 2 Thread ID

Return value: Equals returns a value other than 0, otherwise 0

#include <stdio.h> #include <stdlib.h> #include <pthread.h>int main () {pthread_t thisid;   Thisid = Pthread_self ();   printf ("Current Thread ID =%lu.\n", thisid);   if (Pthread_equal (Thisid,pthread_self ())) {printf ("yes,equal\n");   }else{printf ("No,not equal\n");} return 0;}

3. Create a thread

    • The entry point to invoke the thread function

    • Using the function pthread_create (), after creating the thread, start running the related thread function

function Pthread_create ()

function prototypes: int pthread_create (pthread_t *thread,pthread_attr_t *attr,void* (*start_routine) (void *), void *arg);

Parameters: thread identity; attr thread property setting, usually set to Null;start_routine: The starting address of a thread function, is a pointer to void as a function pointer to a parameter and a return value; ARG: The function passed to Start_runtine

Return value: Successfully returned 0, failed to return failure code

Pthread_create (&tid,null,thrd_func,null)

This article is from the "Superhakce" blog, make sure to keep this source http://superhakce.blog.51cto.com/6671637/1899141

Linux C Programming Multithreading basic 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.