test01.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
int num = 100;
void* Thread1_handler (void* param)
{
while (1)
{
++num;
printf ("Thread1 num=%d\n", num);
}
}
void* Thread2_handler (void* param)
{
while (1)
{
++num;
printf ("Thread2 num=%d\n", num);
}
}
void* Thread3_handler (void* param)
{
while (1)
{
++num;
printf ("Thread3 num=%d\n", num);
}
}
void* Thread4_handler (void* param)
{
while (1)
{
++num;
printf ("Thread4 num=%d\n", num);
}
}
int main ()
{
pthread_t Th1;
pthread_t Th2;
pthread_t Th3;
pthread_t Th4;
Pthread_create (&th1, NULL, Thread1_handler, NULL);
Pthread_create (&th2, NULL, Thread2_handler, NULL);
Pthread_create (&th3, NULL, Thread3_handler, NULL);
Pthread_create (&th4, NULL, Thread4_handler, NULL);
Pthread_join (Th1, NULL);
Pthread_join (Th2, NULL);
Pthread_join (Th3, NULL);
Pthread_join (Th4, NULL);
printf ("exit!\n");
while (1);
}
Test02.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
int num = 100;
pthread_mutex_t mutex1;
void* Thread1_handler (void* param)
{
while (1)
{
Pthread_mutex_lock (&MUTEX1);
++num;
printf ("Thread1 num=%d\n", num);
Pthread_mutex_unlock (&MUTEX1);
}
}
void* Thread2_handler (void* param)
{
while (1)
{
Pthread_mutex_lock (&MUTEX1);
++num;
printf ("Thread2 num=%d\n", num);
Pthread_mutex_unlock (&MUTEX1);
}
}
void* Thread3_handler (void* param)
{
while (1)
{
Pthread_mutex_lock (&MUTEX1);
++num;
printf ("Thread3 num=%d\n", num);
Pthread_mutex_unlock (&MUTEX1);
}
}
void* Thread4_handler (void* param)
{
while (1)
{
Pthread_mutex_lock (&MUTEX1);
++num;
printf ("Thread4 num=%d\n", num);
Pthread_mutex_unlock (&MUTEX1);
}
}
int main ()
{
pthread_t Th1;
pthread_t Th2;
pthread_t Th3;
pthread_t Th4;
Pthread_create (&th1, NULL, Thread1_handler, NULL);
Pthread_create (&th2, NULL, Thread2_handler, NULL);
Pthread_create (&th3, NULL, Thread3_handler, NULL);
Pthread_create (&th4, NULL, Thread4_handler, NULL);
Pthread_join (Th1, NULL);
Pthread_join (Th2, NULL);
Pthread_join (Th3, NULL);
Pthread_join (Th4, NULL);
printf ("exit!\n");
while (1);
}
Test03.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
int num = 100;
pthread_mutex_t mutex1;
void* Thread1_handler (void* param)
{
while (1)
{
Pthread_mutex_lock (&MUTEX1);
++num;
Pthread_mutex_unlock (&MUTEX1);
printf ("Thread1 num=%d\n", num);
}
}
void* Thread2_handler (void* param)
{
while (1)
{
Pthread_mutex_lock (&MUTEX1);
++num;
Pthread_mutex_unlock (&MUTEX1);
printf ("Thread2 num=%d\n", num);
}
}
void* Thread3_handler (void* param)
{
while (1)
{
Pthread_mutex_lock (&MUTEX1);
++num;
Pthread_mutex_unlock (&MUTEX1);
printf ("Thread3 num=%d\n", num);
}
}
void* Thread4_handler (void* param)
{
while (1)
{
Pthread_mutex_lock (&MUTEX1);
++num;
Pthread_mutex_unlock (&MUTEX1);
printf ("Thread4 num=%d\n", num);
}
}
int main ()
{
pthread_t Th1;
pthread_t Th2;
pthread_t Th3;
pthread_t Th4;
Pthread_create (&th1, NULL, Thread1_handler, NULL);
Pthread_create (&th2, NULL, Thread2_handler, NULL);
Pthread_create (&th3, NULL, Thread3_handler, NULL);
Pthread_create (&th4, NULL, Thread4_handler, NULL);
Pthread_join (Th1, NULL);
Pthread_join (Th2, NULL);
Pthread_join (Th3, NULL);
Pthread_join (Th4, NULL);
printf ("exit!\n");
while (1);
}
Test04.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
int num = 100;
pthread_mutex_t mutex1;
pthread_mutex_t Mutex2;
pthread_mutex_t mutex3;
pthread_mutex_t mutex4;
void* Thread1_handler (void* param)
{
while (1)
{
Pthread_mutex_lock (&MUTEX1);
++num;
printf ("Thread1 num=%d\n", num);
Pthread_mutex_unlock (&MUTEX1);
}
}
void* Thread2_handler (void* param)
{
while (1)
{
Pthread_mutex_lock (&MUTEX2);
++num;
printf ("Thread2 num=%d\n", num);
Pthread_mutex_unlock (&MUTEX2);
}
}
void* Thread3_handler (void* param)
{
while (1)
{
Pthread_mutex_lock (&MUTEX3);
++num;
printf ("Thread3 num=%d\n", num);
Pthread_mutex_unlock (&MUTEX3);
}
}
void* Thread4_handler (void* param)
{
while (1)
{
Pthread_mutex_lock (&MUTEX4);
++num;
printf ("Thread4 num=%d\n", num);
Pthread_mutex_unlock (&MUTEX4);
}
}
int main ()
{
pthread_t Th1;
pthread_t Th2;
pthread_t Th3;
pthread_t Th4;
Pthread_create (&th1, NULL, Thread1_handler, NULL);
Pthread_create (&th2, NULL, Thread2_handler, NULL);
Pthread_create (&th3, NULL, Thread3_handler, NULL);
Pthread_create (&th4, NULL, Thread4_handler, NULL);
Pthread_join (Th1, NULL);
Pthread_join (Th2, NULL);
Pthread_join (Th3, NULL);
Pthread_join (Th4, NULL);
printf ("exit!\n");
while (1);
}
Summarize:
Only test02.c is able to ensure that the data is normal, that is, in multi-threaded access to global variables, you must use the same lock on the global variables, and all the operation of the global variable in the lock, even if the operation of the global variable into an atomic operation!
Multithreaded Data security