Pthread User Guide (1) pthread_create

Source: Internet
Author: User

POSIX thread is short for pthread, and Posix thread is a POSIX standard thread. This standard defines internal API creation and thread manipulation. Pthreads defines a set of C program language types, functions, and constants, which are implemented using the pthread. h header file and a thread library.
The code is simple and clear.

 

[Cpp]
# Include <pthread. h>
# Include <stdlib. h>
# Include <stdio. h>
# Include <unistd. h>
# Include <string. h>
 
// Thread ID
Pthread_t ntid;
// Mutex object
Pthread_mutex_t mutex;
 
Int count;
 
Void printids (const char * s)
{
Pid_t pid;
Pthread_t tid;
 
Pid = getpid ();
Tid = pthread_self ();
 
Printf ("% s pid % u tid % u (0x % x) \ n", s, (unsigned int) pid,
(Unsigned int) tid, (unsigned int) tid );
}
 
// Thread Functions
Void * thr_fn (void * arg)
{
Printids ("new thread begin \ n ");
 
// Lock
Pthread_mutex_lock (& mutex );
 
Printids ("new thread :");
 
Int I = 0;
For (; I <5; ++ I)
{
Printf ("thr_fn runing % d \ n", count ++ );
}
 
// Release the mutex lock
Pthread_mutex_unlock (& mutex );
 
Return (void *) 100 );
}
 
Int main (void)
{
Int err;
 
Count = 0;
// Initialize the mutex object
Pthread_mutex_init (& mutex, NULL );
 
// Create a thread
Err = pthread_create (& ntid, NULL, thr_fn, NULL );
If (0! = Err)
{
Printf ("can't create thread: % s \ n", strerror (err ));
}
 
// Sleep (5 );
Pthread_mutex_lock (& mutex );
Printids ("main thread :");
Int I = 0;
For (; I <5; ++ I)
{
Printf ("main runing % d \ n", count ++ );
}
Sleep (1 );
Pthread_mutex_unlock (& mutex );
 
Int ** ret;
Pthread_join (ntid, (void **) ret );
Printf ("thr_fn return % d \ n", * ret );
 
Pthread_mutex_destroy (& mutex );
 
 
Return 0;
}

 


In this example, the pthread data structure is used:
Pthread_t, which identifies the thread ID;
Pthread_mutex_t, which is used for mutual exclusion;


The following functions are used:
Pthread_self (): obtains the thread ID;

Pthread_create (& ntid, NULL, thr_fn, NULL): The thread creates a function.
Prototype: int pthread_create (pthread_t * thread, const pthread_attr_t * attr, void * (* start_routine) (void *), void * arg ).
Tid: ID of the thread for creating a new thread;
Attr: Specifies the thread attribute of the newly created thread. If it is NULL, the default attribute is used;
Start_routine: This is a function pointer that points to a thread function;
Arg: pointer passed to the thread function;
Return Value: 0 indicates success. Failed. The error code is returned.

Pthread_join (ntid, (void *) ret): Wait for the end of a thread
Prototype: int pthread_join (pthread_t thread, void ** retval );
Thread: ID of the thread to be waited;
Retval: stores the return values of the waiting thread;
Return Value: 0 indicates success. Failed. The error code is returned.

Pthread_mutex_init (& mutex, NULL): initializes a mutex lock and dynamically creates a mutex lock.
Prototype: int pthread_mutex_init (pthread_mutex_t * restrict mutex,
Const pthread_mutexattr_t * restrict attr );
Mutex: The Lock Object initialized; www.2cto.com
Attr: Specifies the attribute for creating a mutex lock. If it is NULL, the default mutex lock attribute is used. The default attribute is quick mutex lock.

Pthread_mutex_lock (& mutex): Lock
Pthread_mutex_unlock (& mutex): Release the lock

Pthread_mutex_destroy (& mutex): destroys the Lock Object.


During compilation, you must add the compilation parameter "-lpthread ".
You can block pthread_mutex_lock () and pthread_mutex_unlock () to see the result without mutual exclusion.

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.