Multithreaded Data security

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.