Example: Using two sub-processes to output in descending order from 50
int g_num=50;
Even
void *threadeven (void *lparam)
{
while (g_num>0)
{
if (! ( G_NUM&1))
{
printf ("even:%d\n", g_num);
}
g_num--;
Usleep (1);
}
return NULL;
}
Odd
void *threadodd (void *lparam)
{
while (g_num>0)
{
if (g_num&1)
{
printf ("odd:%d\n", g_num);
}
g_num--;
Usleep (1);
}
return NULL;
}
void Testmutex ()
{
pthread_t thread1,thread2;
int nret1=pthread_create (&thread1,null,threadodd,null);
int nret2=pthread_create (&thread2,null,threadeven,null);
printf ("Thread1 id:%d\t thread2 id:%d\n", (int) thread1, (int) thread2);
if (nret1| | NRet2)
{
fprintf (stderr, "fail to create thread!");
return;
}
Pthread_join (Thread1,null);
Pthread_join (Thread2,null);
}
int main (int argc,char**argv)
{
Testmutex ();
return 0;
}
The above code indicates that two sub-processes are created, the sub-process threadodd output is odd, and the child process Threadeven output an even number. And odd even numbers are alternating outputs. The output is from 50 descending.
If the implementation of odd even separate output, such as: First 50 in the odd or even output. The lock will be added at this time.
Thread Mutex
Multi-threaded shared process resources, access to shared resources, the need for mutually exclusive operations to ensure the validity of data
What is a lock operation:
For example: Two people into the house to take things, a advanced to lock the house, then B will not go in. Only after a lock out, only the turn B go in.
Lock type set/Get
int Pthread_mutexattr_settype (pthread_mutexattr_t* attr, int type);
int Pthread_mutexattr_gettype (pthread_mutexattr_t* attr, int *type);
Type parameter:
Mutex lock type Pthread_mutex_xxx
Normal: Normal lock, do not provide deadlock detection, deadlock may occur
Errorcheck: Wrong lock, the same thread will return an error on the locked mutex lock
RECURSIVE: nested locks, allowing multiple locks on the same thread without deadlock, multiple releases to be locked by other threads
Default: Normal lock, queue get lock
Creating Mutex Locks
Pthread_mutex_init (pthread_mutex_t *mutex, pthread_mutexattr_t *attr)
Attention:
attr = = NULL initialization of the thread lock mutex
Equivalent to Mutex = Pthread_mutex_initializer
Mutex Lock plus lock
Pthread_mutex_lock (pthread_mutex_t *mutex)
Lock the Mutex lock
The same front-line range locks the mutex lock again, causing a deadlock
Mutex lock unlock
Pthread_mutex_unlock (pthread_mutex_t *mutex)
Release Mutex lock
An unlocked mutex, which produces an indeterminate behavior
An attempt to disassociate a mutex created by another thread produces an indeterminate behavior
Pthread_mutex_trylock (pthread_mutex_t *mutex)
Attempt to lock the mutex without blocking
1: The mutex is unlocked, locked and returned to success
2: The mutex is locked and does not block the direct return failure
3: Lock type is nested lock RECURSIVE, locked, then lock counter self-added
Question: Can I query the document without using man? pthread_mutex_* function Family Man no display solution
Workaround: $ sudo apt-get install Manpages-posix-dev
Example: Using a mutex, two sub-processes separate the odd even number of output 50
pthread_mutex_t Mutex;
int g_num=50;
void *threadeven (void *lparam)
{
Locking
Pthread_mutex_lock (&mutex);
while (g_num>0)
{
if (! ( G_NUM&1))
{
printf ("even:%d\n", g_num);
}
g_num--;
Usleep (1);
}
g_num=50;
Unlock
Pthread_mutex_unlock (&mutex);
return NULL;
}
void *threadodd (void *lparam)
{
Locking
Pthread_mutex_lock (&mutex);
while (g_num>0)
{
if (g_num&1)
{
printf ("odd:%d\n", g_num);
}
g_num--;
Usleep (1);
}
g_num=50;
Unlock
Pthread_mutex_unlock (&mutex);
return NULL;
}
void Testmutex ()
{
pthread_t thread1,thread2;
Initialize Mutex lock
Pthread_mutex_init (&mutex,null);
int nret1=pthread_create (&thread1,null,threadodd,null);
int nret2=pthread_create (&thread2,null,threadeven,null);
printf ("Thread1 id:%d\t thread2 id:%d\n", (int) thread1, (int) thread2);
if (nret1| | NRet2)
{
fprintf (stderr, "fail to create thread!");
return;
}
Pthread_join (Thread1,null);
Pthread_join (Thread2,null);
}
int main (int argc,char**argv)
{
Testmutex ();
return 0;
}
The above is implemented, odd even output separately.
Errorcheck and Recursive properties:
Example: Locking lock, Errorcheck error return
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
pthread_mutex_t Mutex;
int g_num=50;
Verifying deadlocks
void *thread ()
{
int Nret=pthread_mutex_lock (&MUTEX);
if (nret)
{
printf ("Repeat lock!\n");
return NULL;
}
printf ("test:%d\n", g_num);
Pthread_mutex_unlock (&mutex);
return NULL;
}
void *threadeven (void *lparam)
{
int Nret=pthread_mutex_lock (&MUTEX);
if (nret)
{
printf ("Repeat lock!\n");
return NULL;
}
while (g_num>0)
{
if (! ( G_NUM&1))
{
printf ("even:%d\n", g_num);
}
g_num--;
Usleep (1);
}
g_num=50;
Thread ();
Pthread_mutex_unlock (&mutex);
return NULL;
}
void *threadodd (void *lparam)
{
int Nret=pthread_mutex_lock (&MUTEX);
if (nret)
{
printf ("Repeat lock!\n");
return NULL;
}
while (g_num>0)
{
if (g_num&1)
{
printf ("odd:%d\n", g_num);
}
g_num--;
Usleep (1);
}
g_num=50;
Thread ();
Pthread_mutex_unlock (&mutex);
return NULL;
}
void Testmutex ()
{
pthread_t thread1,thread2;
pthread_mutexattr_t attr;
Set the properties of a lock
Pthread_mutexattr_settype (&attr,pthread_mutex_recursive);
Pthread_mutexattr_settype (&attr,pthread_mutex_errorcheck);
Default Queue Lock
Pthread_mutex_init (&mutex,null);
Pthread_mutex_init (&MUTEX,&ATTR);
int nret1=pthread_create (&thread1,null,threadodd,null);
int nret2=pthread_create (&thread2,null,threadeven,null);
printf ("Thread1 id:%d\t thread2 id:%d\n", (int) thread1, (int) thread2);
if (nret1| | NRet2)
{
fprintf (stderr, "fail to create thread!");
return;
}
Pthread_join (Thread1,null);
Pthread_join (Thread2,null);
}
int main (int argc,char**argv)
{
Testmutex ();
return 0;
}
25 multi-thread mutual exclusion lock