互斥量未加锁demo
#include<stdio.h>
#include<pthread.h>
// int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
// int pthread_join(pthread_t thread, void **retval);
//void pthread_exit(void *retval);
// int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
int global_data1=1;
pthread_mutex_t mutex;
void *func1(void *arg)
{
int i ;
//pthread_mutex_lock(&mutex);
static int ret=120; //to prevent when func1 quit the ret mabe disappear
printf("the t1_id=%lu\n",pthread_self());
printf("t1 data = %d\n",*((int *)arg));
for(i =0;i<5;i++)
{
printf("%d\n",global_data1++);
}
//pthread_mutex_unlock(&mutex);
pthread_exit((void*)&ret);
}
void *func2(void *arg)
{
int i;
//pthread_mutex_lock(&mutex);
static int ret=99; //to prevent when func1 quit the ret mabe disappear
printf("the t2_id=%lu\n",pthread_self());
printf("t2 data = %d\n",*((int *)arg)+7);
for(i =0;i<5;i++)
{
printf("%d\n",global_data1++);
}
//pthread_mutex_unlock(&mutex);
pthread_exit((void*)&ret);
}
int main ()
{
pthread_t t1;//create thread 1
pthread_t t2;//create thread 2
int data = 100;
int *pretval1=NULL;//use for receive the ti return value
int *pretval2=NULL;//~~~~t2 return value
pthread_mutex_init(&mutex, NULL);
int flag1 = pthread_create(&t1,NULL,func1,(void *)&data);
int flag2 = pthread_create(&t2,NULL,func2,(void *)&data);
if (flag1==0&&flag2==0)
{
printf("t1 and t2 is create succss\n");
printf("main thead_id=%lu\n",pthread_self());
}
pthread_join(t1,(void **)&pretval1);//main stop wait t1 process working done.
pthread_join(t2,(void **)&pretval2);//main stop wait t2 process working done.
printf("func1 retval =%d\n",*pretval1);
printf("func2 retval =%d\n",*pretval2);
pthread_mutex_destroy(&mutex);
return 0;
}
运行结果:
互斥量加锁
#include<stdio.h>
#include<pthread.h>
// int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
// int pthread_join(pthread_t thread, void **retval);
//void pthread_exit(void *retval);
// int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
int global_data1=1;
pthread_mutex_t mutex;
void *func1(void *arg)
{
int i ;
pthread_mutex_lock(&mutex);
static int ret=120; //to prevent when func1 quit the ret mabe disappear
printf("the t1_id=%lu\n",pthread_self());
printf("t1 data = %d\n",*((int *)arg));
for(i =0;i<5;i++)
{
printf("%d\n",global_data1++);
}
pthread_mutex_unlock(&mutex);
pthread_exit((void*)&ret);
}
void *func2(void *arg)
{
int i;
pthread_mutex_lock(&mutex);
static int ret=99; //to prevent when func1 quit the ret mabe disappear
printf("the t2_id=%lu\n",pthread_self());
printf("t2 data = %d\n",*((int *)arg)+7);
for(i =0;i<5;i++)
{
printf("%d\n",global_data1++);
}
pthread_mutex_unlock(&mutex);
pthread_exit((void*)&ret);
}
int main ()
{
pthread_t t1;//create thread 1
pthread_t t2;//create thread 2
int data = 100;
int *pretval1=NULL;//use for receive the ti return value
int *pretval2=NULL;//~~~~t2 return value
pthread_mutex_init(&mutex, NULL);
int flag1 = pthread_create(&t1,NULL,func1,(void *)&data);
int flag2 = pthread_create(&t2,NULL,func2,(void *)&data);
if (flag1==0&&flag2==0)
{
printf("t1 and t2 is create succss\n");
printf("main thead_id=%lu\n",pthread_self());
}
pthread_join(t1,(void **)&pretval1);//main stop wait t1 process working done.
pthread_join(t2,(void **)&pretval2);//main stop wait t2 process working done.
printf("func1 retval =%d\n",*pretval1);
printf("func2 retval =%d\n",*pretval2);
pthread_mutex_destroy(&mutex);
return 0;
}
运行结果:
总结:当一个共享资源(全局变量)被加锁后,只允许当前线程使用,只有当使用完解锁以后,其他线程才可以调用。但这个锁谁先锁上无法保证。
死锁:
#include<stdio.h>
#include<pthread.h>
// int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
// int pthread_join(pthread_t thread, void **retval);
//void pthread_exit(void *retval);
// int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
int global_data1=1;
pthread_mutex_t mutex1;
pthread_mutex_t mutex2;
void *func1(void *arg)
{
int i ;
pthread_mutex_lock(&mutex1);
sleep(1);
pthread_mutex_lock(&mutex2);
static int ret=120; //to prevent when func1 quit the ret mabe disappear
printf("the t1_id=%lu\n",pthread_self());
printf("t1 data = %d\n",*((int *)arg));
for(i =0;i<5;i++)
{
printf("%d\n",global_data1++);
}
pthread_mutex_unlock(&mutex1);
pthread_exit((void*)&ret);
}
void *func2(void *arg)
{
int i;
pthread_mutex_lock(&mutex2);
sleep(1);
pthread_mutex_lock(&mutex1);
static int ret=99; //to prevent when func1 quit the ret mabe disappear
printf("the t2_id=%lu\n",pthread_self());
printf("t2 data = %d\n",*((int *)arg)+7);
for(i =0;i<5;i++)
{
printf("%d\n",global_data1++);
}
pthread_mutex_unlock(&mutex2);
pthread_exit((void*)&ret);
}
int main ()
{
pthread_t t1;//create thread 1
pthread_t t2;//create thread 2
int data = 100;
int *pretval1=NULL;//use for receive the ti return value
int *pretval2=NULL;//~~~~t2 return value
pthread_mutex_init(&mutex1, NULL);
pthread_mutex_init(&mutex2, NULL);
int flag1 = pthread_create(&t1,NULL,func1,(void *)&data);
int flag2 = pthread_create(&t2,NULL,func2,(void *)&data);
if (flag1==0&&flag2==0)
{
printf("t1 and t2 is create succss\n");
printf("main thead_id=%lu\n",pthread_self());
}
pthread_join(t1,(void **)&pretval1);//main stop wait t1 process working done.
pthread_join(t2,(void **)&pretval2);//main stop wait t2 process working done.
printf("func1 retval =%d\n",*pretval1);
printf("func2 retval =%d\n",*pretval2);
pthread_mutex_destroy(&mutex1);
pthread_mutex_destroy(&mutex2);
return 0;
}
运行结果: