首页 > 代码库 > 多线程数据安全
多线程数据安全
//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);
}
总结:
只有Test02.c是能保证数据是正常的,即在多线程访问全局变量时,必须对全局变量使用同一把锁,并且把所有对该全局变量的操作放在锁里,即使对全局变量的操作变成原子操作!
多线程数据安全