A simple example of how to use mutex thread synchronization

Source: Internet
Author: User

 When you start learning thread synchronization, you always think that two threads or multiple threads work together, but that's what you do.

synchronization is a coordinated pace, in order to operate in a predetermined sequence. such as: You finish, I say. The word "Same" is literally easily understood as an action. In fact, the word "the same" should mean synergy, assistance and mutual cooperation. such as process, thread synchronization, can be understood as a process or thread A and b together, a to a certain extent to rely on a result of B, so stop, motioned B to run, B according to the words, and then the result to A;a to continue operation. The so-called synchronization is that when a function call is made, the call does not return until the result is obtained, and no other thread can invoke the method.



Next, a simple example demonstrates the synchronization of threads through mutexes, demonstrating the idea that one thread operates on the data, and the other data outputs the result.



The code is as follows:


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>


pthread_mutex_t Mutex = pthread_mutex_initializer;//defines and initializes the mutex
int Lock_var;
time_t End_time;
void Pthread1 (void *arg);
void pthread2 (void *arg);
int main (int argc, char *argv[])
{
pthread_t Id1,id2;
pthread_t mon_th_id;
int ret;


End_time = Time (NULL) +10;
/* Mutex initialization */
Pthread_mutex_init (&mutex,null);
/* Create two threads */
Ret=pthread_create (&id1,null, (void *) Pthread1, NULL);
if (ret!=0)
Perror ("Pthread cread1");
Ret=pthread_create (&id2,null, (void *) pthread2, NULL);
if (ret!=0)
Perror ("Pthread cread2");


Pthread_join (Id1,null);
Pthread_join (Id2,null);
Exit (0);
}
void Pthread1 (void *arg)
{
int i;
while (Time (NULL) < End_time)
{/* Mutex lock */
if (Pthread_mutex_lock (&mutex)!=0)
{
Perror ("Pthread_mutex_lock");
}
Else
printf ("Pthread1:pthread1 Lock the variable\n");
for (i=0;i<2;i++)
{
Sleep (1);
lock_var++;
}
/* Mutex lock Lock */
if (Pthread_mutex_unlock (&mutex)!=0)
{
Perror ("Pthread_mutex_unlock");
}
Else
printf ("Pthread1:pthread1 unlock the Variable\n");
Sleep (1);
}
}
void pthread2 (void *arg)
{
int nolock=0;
int ret;
while (Time (NULL) < End_time)
{/* Test Mutex */
Ret=pthread_mutex_trylock (&mutex);
if (ret==ebusy)
printf ("Pthread2:the variable is locked by pthread1\n");
Else
{




if (ret!=0)
{
Perror ("Pthread_mutex_trylock");
Exit (1);
}
Else
printf ("Pthread2:pthread2 got lock. The variable is%d\n ", Lock_var); /* Mutex lock Lock */
if (Pthread_mutex_unlock (&mutex)!=0)
{
Perror ("Pthread_mutex_unlock");
}
Else
printf ("Pthread2:pthread2 unlock the Variable\n");
}
Sleep (3);
printf ("++++++++++++++++++++++\n");
}
}

A simple example of how to use mutex thread synchronization

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.