Attribute category
---------------------------------------------
The second parameter of the pthread_create function is about thread attribute settings.
Attributes include bonding attributes, separation attributes, stack address, stack size, and priority. The default stack is non-State, non-separated, 1 MB by default, and has the same priority as the parent process. In pthread_create, if the second parameter is set to null, the default attribute configuration is used.
1) Bonding Property
One-to-one thread mechanism, that is, a user thread corresponds to a kernel thread.
A user thread is allocated to a kernel thread, because the scheduling of CPU time slice is oriented to the kernel thread (Lightweight Process, therefore, a thread with a fixed attribute can ensure that there is always a kernel thread corresponding to it when needed, the non-state attribute corresponds to the relationship between the user thread and the kernel thread, which is not always fixed, but allocated by the system.
2) Separating attributes
The separation attribute determines how to terminate itself.
In the case of non-separation, when a thread ends, the threads that it occupies are not released, that is, they are not actually terminated, and resources need to be released through pthread_join.
In the case of separation of attributes, a thread will immediately release the system resources it occupies when it ends. Note that if you set a thread separation attribute and the thread runs very fast, therefore, it is likely that the running of the thread function will be terminated before the pthread_create function returns. After it is terminated, it is likely that the thread number and system resources will be handed over to other threads for use, then the thread that calls pthread_create gets the wrong thread number.
These attributes are implemented through some functions. Generally, pthread_attr_init is called first, and then corresponding attribute setting functions are called.
Attribute setting function
---------------------------------------------
Pthread_attr_init
Function: Initialize the thread property variable.
Header file: <pthread. h>
Function prototype: int pthread_attr_init (pthread_attr_t * ATTR );
Function input value: ATTR: thread attribute.
Function return value: Success: 0
Failed:-1
Pthread_attr_setscope
Function: sets the thread binding attribute.
Header file: <pthread. h>
Function prototype: int pthread_attr_setscope (pthread_attr_t * ATTR, int scope );
Function input value: ATTR: thread attribute.
Scope: pthread_scope_system (bound)
Pthread_scope_process (not bound)
Function return value: Same as 1.
Pthread_attr_setdetachstate
Function: sets the thread separation attribute.
Header file: <phread. h>
Function prototype: int pthread_attr_setdetachstate (pthread_attr_t * ATTR, int detachstate );
Function input value: ATTR: thread attribute.
Detachstate: pthread_create_detached (separated)
Pthread_create_joinable (non-separated)
Function return value: Same as 1.
Pthread_attr_getschedparam
Function: obtains the thread priority.
Header file: <pthread. h>
Function prototype: int pthread_attr_getschedparam (pthread_attr_t * ATTR, struct sched_param * PARAM );
Function input value: ATTR: thread attribute;
Param: thread priority;
Function return value: Same as 1
Pthread_attr_setschedparam
Function: sets the thread priority.
Header file: <pthread. h>
Function prototype: int pthread_attr_setschedparam (pthread_attr_t * ATTR, struct sched_param * PARAM );
Function input value: ATTR: thread attribute.
Param: Specifies the thread priority.
Function return value: Same as 1
Example
-----------------------------------------------------
# Include <stdlib. h>
# Include <stdio. h>
# Include <errno. h>
# Include <pthread. h>
Static void pthread_func_1 (void );
Static void pthread_func_2 (void );
Int main (INT argc, char ** argv)
{
Pthread_t pt_1 = 0;
Pthread_t pt_2 = 0;
Pthread_attr_t atrr = {0 };
Int ret = 0;
Pthread_attr_init (& ATTR); // initialize attributes
Pthread_attr_setscope (& ATTR, pthread_scope_system );
Pthread_attr_setdetachstate (& ATTR, pthread_create_detached );
Ret = pthread_create (& pt_1, & ATTR, pthread_func_1, null );
If (Ret! = 0)
{
Perror ("pthread_cmdcreate ");
}
Ret = pthread_create (& pt_2, null, pthread_func_2, null );
If (Ret! = 0)
{
Perror ("pthread_2_create ");
}
Pthread_join (pt_2, null );
Return 0;
}
Static void pthread_func_1 (void)
{
Int I = 0;
For (; I <6; I ++)
{
Printf ("this is pthread_1./N ");
If (I = 2)
{
Pthread_exit (0 );
}
}
Return;
}
Static void pthread_func_2 (void)
{
Int I = 0;
For (; I <3; I ++)
{
Printf ("this is pthread_2./N ");
}
Return;
}
The thread function of thread 1 Automatically releases resources at the end of thread 1, and thread 2 has to wait until pthread_join to release system resources.
Original article: http://blog.csdn.net/wtz1985/archive/2009/01/16/3798017.aspx