Summary of linux multithreading (pthread usage)

Source: Internet
Author: User
Summary of linux multithreading (pthread usage)-Linux general technology-Linux programming and kernel information. The following is a detailed description. Original: lobbve223

# Include
Int pthread_create (pthread_t * restrict tidp, const pthread_attr_t * restrict attr,
Void * (* start_rtn) (void), void * restrict arg );
Returns: 0 if OK, error number on failure

The first parameter is the pointer to the thread identifier.

The second parameter is used to set the thread attributes.

The third parameter is the starting address of the thread-running function.

The fourth parameter is the parameter for running the function.

When the thread is successfully created, the function returns 0. If the value is not 0, the thread creation fails. The common error codes returned are EAGAIN and EINVAL. The former indicates that the system restricts the creation of new threads. For example, the number of threads is too large. The latter indicates that the second parameter indicates that the thread attribute value is invalid.
Usage of pthread_create: Because the pthread library is not the default library in Linux, when using pthread_create to create a thread, you must add the-lpthread parameter to the compilation. For example: gcc-o test-lpthrea test. c

Example 1:

# Include "pthread. h"
# Include "stdio. h"
Void * thread_test (void * ptr)
{While (1)
Printf ("I am pthread \ n ");
}
Int main ()
{
Pthread_t pid;
Pthread_create (& pid, NULL, test_thread, NULL );
While (1)
Printf ("I am main pthread \ n ");
Return 0;
}

Example 2:

# Include
# Include
Pthread_t id;
Int ret;
Void thread_1 ()
{
While (1)
{Printf ("I am thread \ n ");
Sleep (1 );
}
}
Main ()
{Ret = pthread_create (& id, NULL, (void *) thread_1, NULL );
If (ret! = 0)
Printf ("Create pthread error! \ N ");
While (1)
{
Printf ("I am main thread \ n ");
Sleep (2 );
}
}

Example 3:

# Include
# Include
# Include
# Include
Void * thread_function (void * arg );
Char message [] = "Hello World ";
Int main ()
{
Int res;
Pthread_t a_thread;
Void * thread_result;
Res = pthread_create (& a_thread, NULL, thread_function, (void *) message );
If (res! = 0)
{
Perror ("Thread creation failed ");
Exit (EXIT_FAILURE );
}
Printf ("Waiting for thread to finish... \ n ");
Res = pthread_join (a_thread, & thread_result); // pthread_join: the thread that blocks execution until a thread ends.
If (res! = 0)
{
Perror ("Thread join failed ");
Exit (EXIT_FAILURE );
}
Printf ("Thread joined, it returned % s \ n", (char *) thread_result );
Printf ("Message is now % s \ n", message );
Exit (EXIT_SUCCESS );
}
Void * thread_function (void * arg)
{
Printf ("thread_function is running. Argument was % s \ n", (char *) arg );
Sleep (3 );
Strcpy (message, "Bye! ");
Pthread_exit ("Thank you for the CPU time ");
}
[Root @ plinux tmp] # cc-D_REENTRANT-I/usr/include/nptl thread2.c-o thread2-L/usr/lib/nptl-lpthread
[Root @ plinux tmp] #./thread2
Thread_function is running. Argument was Hello World
Waiting for thread to finish...
Thread joined, it returned Thank you for the CPU time
Message is now Bye!
Pthread_join ()
Void pthread_exit (void * retval)
Int pthread_join (pthread_t pid, void ** thread_return)

The caller of pthread_join () will suspend and wait for th thread to terminate. retval is the return value of the thread that calls pthread_exit () (thread ID is pid). If thread_return is not NULL, * thread_return = retval.

It should be noted that one thread only allows the only other thread to use pthread_join () to wait for the termination of this thread, and the waiting thread should be in the join state, that is, non-DETACHED state.
Related 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.