Linux multi-thread programming basic functions

Source: Internet
Author: User
Article title: Linux multi-threaded programming basic functions. Linux is a technology channel of the IT lab in China. Including desktop applications, Linux system management, kernel research, embedded systems, open source, and other basic categories 1 thread creation

Function prototype:


# Include
Int pthread_create (pthread_t * restrict tidp, const pthread_attr_t * restrict attr, void * (* start_rtn) (void), void * restrict arg );

Returned value: if a thread is successfully created, 0 is returned. Otherwise, the error number is returned.
Format parameters:
Pthread_t * restrict tidp indicates the thread id pointer of the thread to be created
Const pthread_attr_t * restrict attr specifies the thread attributes when a thread is created.
Void * (start_rtn) (void) returns a void pointer function.
Vodi * restrict arg start_rtn row parameter
Example 1:
Function: Test the creation of a new thread.
Program Name: pthread_test.c

# Include
# Include
Void * create (void * arg)
...{
Printf ("new thread created .....");

}
Int main (int argc, char * argv [])
...{
Pthread_t tidp;
Int error;

Error = pthread_create (& tidp, NULL, create, NULL );
If (error! = 0)
......{
Printf ("pthread_create is not created ...");
Return-1;
}
Printf ("prthread_create is created ...");
Return 0;
}

Compilation method:

# Gcc-Wall-lpthread pthread_test.c

Because the pthread library is not a linux system library, you must add-lpthread during compilation. Otherwise, the compilation will fail and the following error will occur.

Thread_test.c:In the 'create' function'Medium:
Thread_test.c: 7:Warning in a function with returned values, the program flow reaches the end of the function.
/Tmp/ccOBJmuD. o: In function 'main': thread_test.c :(. text + 0x4f): For 'pthread _ create'Undefined reference
Collect2: ldReturn 1

[1] [2] [3] [4] [5] [6] Next page

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.