Linux多线程——使用互斥量同步线程
- #include <pthread.h>
- int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr);
- int pthread_mutex_lock(pthread_mutex_t *mutex);
- int pthread_mutex_unlock(pthread_mutex_t *mutex);
- int pthread_mutex_destroy(pthread_mutex_t *mutex);
- #include <unistd.h>
- #include <pthread.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- //声明线程函数和互斥量
- void* thread_func(void *msg);
- pthread_mutex_t mutex;
- #define MSG_SIZE 512
- int main()
- {
- int res = -1;
- pthread_t thread;
- void *thread_result = NULL;
- char msg[MSG_SIZE] = {'\0'};
- //初始化互斥量,使用默认的互斥量属性
- res = pthread_mutex_init(&mutex, NULL);
- if(res != 0)
- {
- perror("pthread_mutex_init failed\n");
- exit(EXIT_FAILURE);
- }
- //创建子线程,并把msg作为线程函数的参数传递给thread_func
- res = pthread_create(&thread, NULL, thread_func, msg);
- if(res != 0)
- {
- perror("pthread_create failed\n");
- exit(EXIT_FAILURE);
- }
- //输入字符串,以串‘end’结束
- printf("Input some test. Enter 'end' to finish\n");
- //把互斥量mutex加锁,以确保同一时间只有该线程可以访问msg中的数据
- pthread_mutex_lock(&mutex);
- while(strcmp("end\n", msg) != 0)
- {
- if(strncmp("TEST", msg, 4) == 0)
- {
- strcpy(msg, "copy_data\n");
- }
- else
- {
- fgets(msg, MSG_SIZE, stdin);
- }
- //把互斥量mutex解锁,让其他的线程可以访问msg中的数据
- pthread_mutex_unlock(&mutex);
- sleep(1);//休眠1秒再继续循环,让其他线程有执行的机会
- pthread_mutex_lock(&mutex);
- }
- pthread_mutex_unlock(&mutex);
- printf("\nWaiting for thread finish...\n");
- //等待子线程结束
- res = pthread_join(thread, &thread_result);
- if(res != 0)
- {
- perror("pthread_join failed\n");
- exit(EXIT_FAILURE);
- }
- printf("Thread joined\n");
- //清理互斥量
- pthread_mutex_destroy(&mutex);
- exit(EXIT_SUCCESS);
- }
- void* thread_func(void *msg)
- {
- int i = 0;
- char *ptr = msg;
- sleep(1);
- //把互斥量mutex加锁,以确保同一时间只有该线程可以访问msg中的数据
- pthread_mutex_lock(&mutex);
- while(strcmp("end\n", msg) != 0)
- {
- //把小写字母变成大写
- for(i = 0; ptr[i] != '\0'; ++i)
- {
- if(ptr[i] >= 'a' && ptr[i] <='z')
- {
- ptr[i] -= 'a' - 'A';
- }
- }
- printf("You input %d characters\n", i-1);
- printf("To uppercase: %s\n", ptr);
- //把互斥量mutex解锁,让其他的线程可以访问msg中的数据
- pthread_mutex_unlock(&mutex);
- sleep(1);//休眠1秒再继续循环,让其他线程有执行的机会
- pthread_mutex_lock(&mutex);
- }
- pthread_mutex_unlock(&mutex);
- //退出线程
- pthread_exit(NULL);
- }
undefined reference to 'pthread_join'
在编译中要加 -lpthread参数
gcc thread.c -o thread -lpthread
thread.c为你些的源文件,不要忘了加上头文件#include<pthread.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>//声明线程函数和互斥量
void* thread_func(void *msg);
pthread_mutex_t mutex;#define MSG_SIZE 512int main()
{int res = -1;pthread_t thread;void *thread_result = NULL;char msg[MSG_SIZE] = {'\0'};//初始化互斥量,使用默认的互斥量属性res = pthread_mutex_init(&mutex, NULL);if(res != 0){perror("pthread_mutex_init failed\n");exit(EXIT_FAILURE);}//创建子线程,并把msg作为线程函数的参数传递给thread_funcres = pthread_create(&thread, NULL, thread_func, msg);if(res != 0){perror("pthread_create failed\n");exit(EXIT_FAILURE);}//输入字符串,以串‘end’结束printf("Input some test. Enter 'end' to finish\n");//把互斥量mutex加锁,以确保同一时间只有该线程可以访问msg中的数据pthread_mutex_lock(&mutex);while(strcmp("end\n", msg) != 0){if(strncmp("TEST", msg, 4) == 0)/*输入的msg是TEST*/{printf("www=============================test1\n");strcpy(msg, "copy_data\n");}else/*输入的msg不是TEST*/{printf("www=============================test2\n");fgets(msg, MSG_SIZE, stdin);/*等待用户输入*/}//把互斥量mutex解锁,让其他的线程可以访问msg中的数据pthread_mutex_unlock(&mutex);sleep(1);//休眠1秒再继续循环,让其他线程有执行的机会pthread_mutex_lock(&mutex);}pthread_mutex_unlock(&mutex);printf("\nWaiting for thread finish...\n");//等待子线程结束res = pthread_join(thread, &thread_result);if(res != 0){perror("pthread_join failed\n");exit(EXIT_FAILURE);}printf("Thread joined\n");//清理互斥量pthread_mutex_destroy(&mutex);exit(EXIT_SUCCESS);
}
void* thread_func(void *msg)/*子线程的功能是把主线程的传下来的字母变成大写字母*/
{int i = 0;char *ptr = msg;sleep(1);//把互斥量mutex加锁,以确保同一时间只有该线程可以访问msg中的数据pthread_mutex_lock(&mutex);while(strcmp("end\n", msg) != 0){//把小写字母变成大写for(i = 0; ptr[i] != '\0'; ++i){if(ptr[i] >= 'a' && ptr[i] <='z'){ptr[i] -= 'a' - 'A';}}printf("You input %d characters\n", i-1);printf("To uppercase: %s\n", ptr);//把互斥量mutex解锁,让其他的线程可以访问msg中的数据pthread_mutex_unlock(&mutex);sleep(1);//休眠1秒再继续循环,让其他线程有执行的机会pthread_mutex_lock(&mutex);}pthread_mutex_unlock(&mutex);//退出线程pthread_exit(NULL);
}
wqf@wqf-System-Product-Name:/worksen/lddexamples/muax$ gcc muax.c -o muax -lpthread
wqf@wqf-System-Product-Name:/worksen/lddexamples/muax$ ./muax
Input some test. Enter 'end' to finish
www=============================test2
qqq
You input 3 characters
To uppercase: QQQwww=============================test2
TEST
You input 4 characters
To uppercase: TESTwww=============================test1
You input 9 characters
To uppercase: COPY_DATAwww=============================test2
eee
You input 3 characters
To uppercase: EEEwww=============================test2
endWaiting for thread finish...
Thread joined
wqf@wqf-System-Product-Name:/worksen/lddexamples/muax$
Linux多线程——使用互斥量同步线程相关推荐
- linux 只运行一个实例 互斥锁,Linux多线程4-1_互斥量
//包含头文件 int pthread_mutex_destroy(pthread_mutex_t *mutex); int pthread_mutex_init(pthread_mutex_t *r ...
- Visual C++利用互斥量同步线程实现文件读取进度条
忘了原文的位置了. 一.前言 文件读取进度条的实现可以有很多种方法,常用的是在读取文件的过程中隔一定时间向对话框发送消息以控制进度条的位置,但是这种方法很难确定隔多少时问发送一个消息,因为文件的大小是 ...
- 多线程编程(3): 使用互斥锁同步线程
python多线程编程(3): 使用互斥锁同步线程 问题的提出 上一节的例子中,每个线程互相独立,相互之间没有任何关系.现在假设这样一个例子:有一个全局的计数num,每个线程获取这个全局的计数,根据n ...
- linux多线程拷贝信号量运用于线程之间通讯遇到的printf问题
linux多线程拷贝信号量运用于线程之间通讯遇到的printf问题 1.题目内容 使用多线程实现文件的拷贝,并使用进度条进行相应的显示 2.进度 以下代码实现了多线程完成文件的拷贝,但是在使用进度条实 ...
- python多线程编程(2): 使用互斥锁同步线程
上一节的例子中,每个线程互相独立,相互之间没有任何关系.现在假设这样一个例子:有一个全局的计数num,每个线程获取这个全局的计数,根据num进行一些处理,然后将num加1.很容易写出这样的代码: # ...
- python 线程锁_python多线程编程(3): 使用互斥锁同步线程
问题的提出 上一节的例子中,每个线程互相独立,相互之间没有任何关系.现在假设这样一个例子:有一个全局的计数num,每个线程获取这个全局的计数,根据num进行一些处理,然后将num加1.很容易写出这样的 ...
- python多线程编程(3): 使用互斥锁同步线程
问题的提出 上一节的例子中,每个线程互相独立,相互之间没有任何关系.现在假设这样一个例子:有一个全局的计数num,每个线程获取这个全局的计数,根据num进行一些处理,然后将num加1.很容易写出这样的 ...
- python多线程同步与互斥_python多线程编程(3): 使用互斥锁同步线程
问题的提出 上一节的例子中,每个线程互相独立,相互之间没有任何关系.现在假设这样一个例子:有一个全局的计数num,每个线程获取这个全局的计数,根据num进行一些处理,然后将num加1.很容易写出这样的 ...
- linux c进程线程的面试问题,linux 多线程面试题_linux进程线程_linux 线程 pthread_create...
你写了一个简单的mandelbrot set程序,也就是说用ps命令行是可以看见多个线程,win32里同一个进程里各个线程之间是共享数据段的,win32的进程管理方式与unix上有着很大区别.adob ...
最新文章
- 当程序出Bug时,程序员最喜欢说的30句话
- OKR落地,实践经验总结两个点比较重要
- 标题h和img优化的技巧
- 关于sigma pix的理解
- OpenCV GPU 简单遍历图像
- ppt批量缩略图_PPT如何在文件夹下显示缩略图的方法
- 搭建webpack基础配置
- WSSv3 Technical Articles_Windows SharePoint Services 3.0编码开发工具和技巧(Part 2 of 2)
- pyspark对应的scala代码PythonRDD类
- React中useEffect使用
- git向远程推送代码提示fatal: Authentication failed
- Ubuntu部署KVM服务器
- 【干货摘录】和秋叶一起学PPT书本中干货集合、PPT设计必备网站、找字体网站
- JTAG接口定义与其他简介
- week15-字符串
- POJ 1129 Channel Allocation(四色定理)
- 移动魔百盒CM201-2,YS代工-免拆机-直刷固件及教程
- 技术管理规划-如何设定团队的目标
- 华为云添加HSS云主机agent无法安装提示已存在问题处理
- 高德的位置服务器,高德位置服务浅析
热门文章
- Oracle PL/SQL匿名块(三)
- php 逗号 分割字符串
- 教你如何防止电脑插入u盘后自动运行
- 使用pandas进行量化回测(akshare)
- 基于matlab的大米,大米颗数计算MATLAB软件
- python发展历程
- 华为2017java笔试题_2017年java华为面试题
- [c++]代理对象模式
- php实现服务器文件同步,PHPstorm配置同步服务器文件
- h5 android数字键盘,【笔记】移动端H5数字键盘input type=number的处理(IOS和Android)...