编辑代码

#if 0
//编译时需要加-lpthread参数
#include<stdio.h>  
#include<pthread.h>  
#include<stdlib.h>
#include <unistd.h>
  
void  fun_thread1(char * msg);  
void  fun_thread2(char * msg);  
int g_value = 1;  
pthread_mutex_t mutex;  
/*in the thread individual, the thread reset the g_value to 0,and add to 5 int the thread1,add to 6 in the thread2.*/  
int main(int argc, char * argv[])  
{  
    pthread_t thread1;  
    pthread_t thread2;  
    if(pthread_mutex_init(&mutex,NULL) != 0 )  //该函数用于C函数的多线程编程中,互斥锁的初始化
    {  
        printf("Init metux error.");  
        exit(1);  
    }  
    if(pthread_create(&thread1,NULL,(void *)fun_thread1,NULL) != 0)  
    {  
        printf("Init thread1 error.");  
        exit(1);  
    }  
    if(pthread_create(&thread2,NULL,(void *)fun_thread2,NULL) != 0)  
    {  
        printf("Init thread2 error.");  
        exit(1);  
    }  
    sleep(1);  
    printf("I am main thread, g_vlaue is %d./n",g_value);  
    return 0;  
}  
void  fun_thread1(char * msg)  
{  
    int val;  
    val = pthread_mutex_lock(&mutex);/*lock the mutex*/  
    if(val != 0)  
    {  
        printf("lock error.");  
    }  
    g_value = 0;/*reset the g_value to 0.after that add it to 5.*/  
    printf("thread 1 locked,init the g_value to 0, and add 5.\n");  
    g_value += 5;  
    printf("the g_value is %d.\n",g_value);  
    pthread_mutex_unlock(&mutex);/*unlock the mutex*/  
    printf("thread 1 unlocked.\n");  
}  
void  fun_thread2(char * msg)  
{     
    int val;  
    val = pthread_mutex_lock(&mutex);/*lock the mutex*/  
    if(val != 0)  
    {  
        printf("lock error.");  
    }  
    g_value = 0;/*reset the g_value to 0.after that add it to 6.*/  
    printf("thread 2 locked,init the g_value to 0, and add 6.\n");  
    g_value += 6;  
    printf("the g_value is %d.\n",g_value);  
    pthread_mutex_unlock(&mutex);/*unlock the mutex*/  
    printf("thread 2 unlocked.\n");  
}  

#else

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

pthread_mutex_t mutex;

int file(int a)
{
	char temp[64] = {0};
	FILE *fp = NULL;
	fp = fopen("./xiancheng.txt","a+");
	if(fp == NULL)
	{
		printf("open file err\n");
		return -1;
	}
	else
	{
		printf("open success\n");
		sprintf(temp,"This is %d write file\n",a);
		fprintf(fp,"%s",temp);
		fclose(fp);
		return 0;
	}
}

void *pth_fun(void *pth_arg){
    while(1){
        printf("11111\n");
        sleep(1);
    }
    return NULL;
}

void *pth_fun_1(void *pth_arg){
	int a = 0;
    while(1){
        printf("2222222222\n");
		int val;  
		/* val = pthread_mutex_lock(&mutex);//lock the mutex  
		if(val != 0)  
		{  
			printf("lock error.");  
		}    */
		file(a++);
		//pthread_mutex_unlock(&mutex);/*unlock the mutex*/  
		//printf("thread 1 unlocked.\n");	
        sleep(1);
    }
    return NULL;
}

void *pth_fun_2(void *pth_arg){
    while(1){
        printf("3333333333\n");
        sleep(1);
    }
    return NULL;
}

int main(int argc, char const *argv[])
{
	FILE *fp = NULL;
	fp = fopen("./xiancheng.txt","w+");
	if(fp == NULL)
	{
		printf("empty file err\n");
		return -1;
	}
	fclose(fp);
    pthread_t tid=0;
	pthread_t tid_1=0;
	pthread_t tid_2=0;
	
	if(pthread_mutex_init(&mutex,NULL) != 0 )  //该函数用于C函数的多线程编程中,互斥锁的初始化
    {  
        printf("Init metux error.");  
        exit(1);  
    }  
	
    pthread_create(&tid,NULL,pth_fun,NULL);
	pthread_create(&tid_1,NULL,pth_fun_1,NULL);
	pthread_create(&tid_2,NULL,pth_fun_2,NULL);
    printf("tid=%lu,tid_1=%lu,tid_2=%lu\n",tid,tid_1,tid_2); //typedef unsigned long int pthread_t;
	//%u 是unsigned int
	//%lu 是unsigned long int
	
    while(1){
        printf("hhhhhhhhhhh\n");
        sleep(3);
        printf("-----------------\n");
    }

    return 0;
}
#endif