• 利用互斥锁实现多个线程写一个文件


    代码

    #include 
    #include 
    #include 
    #include 
    
    FILE *fp;
    
    //线程函数1
    void *wrfunc1(void *arg);
    //线程函数2
    void *wrfunc2(void *arg);
    //线程函数3
    void *wrfunc3(void *arg);
    
    //静态创建互斥锁
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    
    int main(int argc, char *argv[])
    {
    	//三个线程id
    	pthread_t tid1;
    	pthread_t tid2;
    	pthread_t tid3;
    	//三个线程参数
    	int arg1 = 1;
    	int arg2 = 2;
    	int arg3 = 3;
    	//三个线程返回值
    	void *ret1;
    	void *ret2;
    	void *ret3;
    
    	if ((fp = fopen("test.txt", "a+")) == NULL)
    	{
    		perror("fopen");
    	}
    
    	//创建三个线程
    	pthread_create(&tid1, NULL, wrfunc1, &arg1);
    	sleep(2);
    	pthread_create(&tid2, NULL, wrfunc2, &arg2);
    	sleep(2);
    	pthread_create(&tid3, NULL, wrfunc3, &arg3);
    	sleep(2);
    
    	//延长休眠时间,保证三个线程都有机会执行
    	sleep(100);
    
    	//取消线程
    	pthread_cancel(tid1);
    	pthread_cancel(tid2);
    	pthread_cancel(tid3);
    
    	//回收线程
    	pthread_join(tid1, &ret1);
    	pthread_join(tid2, &ret2);
    	pthread_join(tid3, &ret3);
    
    	return 0;
    }
    
    void *wrfunc1(void *arg)
    {	
    	char str[] = "I am thread1, I write a line\n"; //线程要写入文件的字符串
    	int i;
    	
    	printf("this is thread%d\n", *(int*)arg);
    	while (1)
    	{
    		pthread_mutex_lock(&mutex); //加入互斥锁
    		
    		//按字符将字符串循环写入文件
    		for (i = 0; i < strlen(str); i++)
    		{
    			fputc(str[i], fp);
    			usleep(1);
    		}
    		pthread_mutex_unlock(&mutex); //释放互斥锁
    		pthread_testcancel(); //设置线程取消点
    	}
    
    	sleep(10);
    	pthread_exit("thread1 return"); //退出线程
    }
    
    void *wrfunc2(void *arg)
    {
    	char str[] = "I am thread2, I write a line\n";
    	int i;
    
    	printf("this is thread%d\n", *(int*)arg);
    	while (1)
    	{
    		pthread_mutex_lock(&mutex);
    		for (i = 0; i < strlen(str); i++)
    		{
    			fputc(str[i], fp);
    			usleep(1);
    		}
    		pthread_mutex_unlock(&mutex);
    		pthread_testcancel();
    	}
    
    	sleep(10);
    	pthread_exit("thread2 return");
    }
    
    void *wrfunc3(void *arg)
    {
    	char str[] = "I am thread3, I write a line\n";
    	int i;
    
    	printf("this is thread%d\n", *(int*)arg);
    	while (1)
    	{
    		pthread_mutex_lock(&mutex);
    		for (i = 0; i < strlen(str); i++)
    		{
    			fputc(str[i], fp);
    			usleep(1);
    		}
    		pthread_mutex_unlock(&mutex);
    		pthread_testcancel();
    	}
    
    	sleep(10);
    	pthread_exit("thread3 return");
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129

    总结

    • 在这个代码中,是需要加入 pthread_cancel() 函数的因为:线程函数wrfunc1、wrfunc2和wrfunc3都包含一个无限循环,循环内部没有明确的退出条件。没有明确的退出条件意味着线程函数会一直执行下去,直到程序结束或者发生了某个特殊的情况导致线程被强制终止。在这种情况下,如果没有使用pthread_cancel,线程将一直运行下去,不会自动退出。
    • 这个代码中的 sleep(100) 是必须加入的,如果不加入那么 线程 2 和 线程 3 将没有机会执行,也就看不到正确的结果。
  • 相关阅读:
    MySQL---单行函数
    不生成DOM的非主流Blazor UI开源啦!
    Java学习—Collections工具类
    SpringBoot-EasyExcel(大数据处理)
    docker-compose概述与简单编排部署
    【服务器数据恢复】EMC Unity存储误操作删除数据卷的数据恢复案例
    在html和css中的引用svg(一)
    张成方案——Span Programs
    Element UI库 之 el-upload 图片上传组件多次上传或重新上传失效bug
    艾美捷热转移稳定性检测试剂盒参数&文献参考
  • 原文地址:https://blog.csdn.net/qq_44947439/article/details/133700525