Pthread_mutex_lock thread Lock Use simple example

Source: Internet
Author: User
Tags usleep

#define __use_largefile64
#define _largefile64_source
#ifndef _gnu_source
#define _gnu_source
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <pthread.h>
#include ". /errors.h "
#define Mutex_lock


pthread_mutex_t Mutex;
void *thread_function_even (void *inpara)
{
int *num = (int *) Inpara;
Long unsigned int thread_num = Pthread_self ();
Pthread_detach (Thread_num); /*detach to is an independent thread*/
while (1)
{
#ifdef Mutex_lock
Pthread_mutex_lock (&mutex);
#endif
*num = (*num * 2)% 100;
Usleep (1);
printf ("int thread_function_even,num:%d,is even:%d\n", *num, (0 = = *num% 2));
#ifdef Mutex_lock
Pthread_mutex_unlock (&mutex);
#endif
}
}
void *thread_function_odd (void *inpara)
{
int *num = (int *) Inpara;
Long unsigned int thread_num = Pthread_self ();
Pthread_detach (Thread_num); /*detach to is an independent thread*/
while (1)
{
#ifdef Mutex_lock
Pthread_mutex_lock (&mutex);
#endif
*num = (*num * 2)% 100 + 1;
Usleep (1);
printf ("int thread_function_odd,num:%d,is even:%d\n", *num, (0 = = *num% 2));
#ifdef Mutex_lock
Pthread_mutex_unlock (&mutex);
#endif
}
}
int main (int argc, char *argv[])
{
/*mutex is a lock,everyone only can operate an array by get the mutex*/
#ifdef Mutex_lock
Pthread_mutex_init (&mutex, NULL);
#endif


/*create the threads,one change the NUM to even,the and the NUM to odd*/
int i = 0;
pthread_t thread_even,thread_odd;
Pthread_create (&thread_even, NULL, Thread_function_even, &i);
Pthread_create (&thread_odd, NULL, thread_function_odd, &i);


while (1);
#ifdef Mutex_lock
Pthread_mutex_destroy (&mutex);
#endif

}

1. Compilation: GCC main.c-lpthread-g

2. Run:./a > 10 seconds after ctrl C stops

3. Verify that the lock is valid, the following two commands do not search the result:

Cat 1|grep Thread_function_odd|grep "Even:1"
Cat 1|grep Thread_function_even|grep "even:0"

4. Remove the lock in the code: note # define Mutex_lock

5. After removing the verification is not locked after the correct, the two command in step 3 can search the results

Cat 1|grep Thread_function_odd|grep "Even:1"
Cat 1|grep Thread_function_even|grep "even:0"

6. The principle of the code: Create two threads, two threads all operate the same integer, an integer only to an even number, an integer only to the odd, lock the case, the even-numbered threads print all the numbers are even, odd-numbered threads are all odd numbers printed

Pthread_mutex_lock thread Lock Use simple example

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.