Copy Code code as follows:
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t Device_mutex;
int count=0;
void Thread_func1 ()
{
while (1)
{
Pthread_mutex_lock (&device_mutex);
printf ("Thread1:%d\n", count);
Pthread_mutex_unlock (&device_mutex);
count++;
Sleep (1);
}
}
void Thread_func2 ()
{
while (1)
{
Pthread_mutex_lock (&device_mutex);
printf ("Thread2:%d\n", count);
Pthread_mutex_unlock (&device_mutex);
count++;
Sleep (1);
}
}
int main ()
{
pthread_t Thread1, Thread2;
Pthread_mutex_init (&device_mutex,null);
if (Pthread_create &thread1,null, (void*) thread_func1,null) = = 1)
{
printf ("Create IP81 Thread error!\n");
Exit (1);
}
Sleep (1);
if (Pthread_create (&thread2,null, (void *) thread_func2,null) = = 1)
{
printf ("Create ip81_2 Thread error!\n");
Exit (1);
}
Sleep (1);
Pthread_join (Thread1,null);
Pthread_join (Thread2,null);
Pthread_mutex_destroy (&device_mutex);
return 0;
}