Linux c 多线程写日志,linux c/c++多线程程序的编写(转)
第一次尝试编写linux下多线程程序,也是颇废一番周折。深知初学者对能快速的成功运行一段程序的渴望,所以本文专门用了“linux
c/c++多线程程序的编写“这个醒目的标题,方便大家能即使从搜索引擎搜到本文,从而提升大家的信心。
首先先让代码进行起来再说为什么,见了效果再回过来头来看原因似乎会容易明白和理解一些。
测试代码如下:
#include[stdio.h]
#include[unistd.h]
#include[stdlib.h]
#include[pthread.h]
#include[string.h]
void *thread_function(void* arg);
char message[] = "Hello world!";
int main() {
int res;
pthread_t a_thread;
void *thread_result;
res = pthread_create(&a_thread, NULL, thread_function, (void*)message);
if (0 != res) {
perror("Thread creation faied");
exit(EXIT_FAILURE);
}
printf("Waiting for thread to finish...\n");
res = pthread_join(a_thread, &thread_result);
if (0 != res) {
perror("Thread join failed");
exit(EXIT_FAILURE);
}
printf("Thread joined, it returned %s\n", (char*)thread_result);
printf("Message is now %s\n",message);
exit(EXIT_FAILURE);
}
void *thread_function(void* arg) {
printf("thread_function is running. argument was %s\n",(char*)arg);
sleep(3);
strcpy(message,"bye!");
pthread_exit("Thank you for the cpu time");
}
请将包含头文件的[替换成<
,]替换成>,代码高亮插件会认为尖括号是html标签,所以这里用中括号暂时替代。
编译:
hqlong@ubuntu:~/code/c$ gcc test.c -o test -lpthread
运行
hqlong@ubuntu:~/code/c$ ./test
结果
thread_function is running. argument was Hello world!
Waiting for thread to finish...
Thread joined, it returned Thank you for the cpu time
Message is now bye!
创建进程主要是通过pthread_create()这个函数来创建,函数的定义如下:
int pthread_create
(
pthread_t *restrict tidp,
const pthread_attr_t *restrict attr,
void *(*start_rtn)(void),
void *restrict arg
);
返回值:若是成功建立线程返回0,否则返回错误的编号
形式参数:
pthread_t *restrict tidp 要创建的线程的线程id指针
const pthread_attr_t *restrict attr 创建线程时的线程属性
void* (start_rtn)(void) 返回值是void类型的指针函数
vodi *restrict arg start_rtn的行参
注意上面还用到了pthread_join()这个函数,那么这个函数有什么用呢?接下来还是先看看它的原形定义:
int pthread_join
(
pthread_t thread,
void **value_ptr
);
各参数说明如下:
thread 等待退出线程的线程号
value_ptr 退出线程的返回值。
该函数的作用使得当前进程挂起,等待另一个进程返回才继续执行。也就是说当程序运行到这个地方时,程序会先停止,然后等线程id为thread的这个线程返回,然后程序才会断续执行。
在上面的例子中,如果把
res = pthread_join(a_thread, &thread_result);
if (0 != res) {
perror("Thread join failed");
exit(EXIT_FAILURE);
}
printf("Thread joined, it returned %s\n", (char*)thread_result);
注释掉,那么主程序根据不会等待线程返回,就退出,当然主进行退出了,他所创建的子进程也就会跟着退出,所以在上面的例子中,程序还在睡眠时,就被主进程强行退出了。
在上面例子进对源码进行编译时,加了-lpthread,如果不加-lprhead,会出现如下编译错误。
tmp/ccnPVTWo.o: In function `main':
mthread.c:(.text+0x30): undefined reference to `pthread_create'
mthread.c:(.text+0x6f): undefined reference to `pthread_join'
collect2: ld returned 1 exit status
thread
库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a。
Linux c 多线程写日志,linux c/c++多线程程序的编写(转)相关推荐
- python多线程写日志_python 应用logging多线程写多个log文件
python 使用logging多线程写多个log文件 import threading import time import logging import logging.handlers LEVE ...
- linux 多线程 写日志,rsyslog多线程远程日志记录介绍(lamp+rsyslog)
rsyslog多线程远程日志记录介绍(lamp+rsyslog) rsyslog: rsyslog: 多线程: 支持UDP, TCP, SSL, TLS, RELP远程日志记录 rsyslog支持将日 ...
- linux通过时间查询日志,linux按时间查询日志
在系统应用集中部署的时候,很多日志因为太多难以定位,获取某段时间的日志是对运维人员非常关键的事情. 一.sed查看某时间段到现在的系统日志: sed -n '/May 20 17/,$p' / ...
- linux 进程被杀日志,Linux查看日志和关闭、杀***进程命令
1 -----Linux查看tomcat日志命令: cd /:切换到根目录 ll:查看根目录下的文件 cd /data/kafu/log:切换到日志目录 ll:查看日志目录下的文件 ta ...
- linux不写日志,linux 重要日志
以下介绍的是20个位于/var/log/ 目录之下的日志文件.其中一些只有特定版本采用,如dpkg.log只能在基于Debian的系统中看到. /var/log/messages - 包括整体系统信息 ...
- linux+top写日志,Linux:日志那些命令
这里都是Linux那些命令 1.sed: 2.awk: 3.printf: 4.sort 5.cut: # linux 下 取进程占用 cpu 最高的前10个进程 ps aux|head -1;ps ...
- linux中写入log日志,Linux往log中写日志
void writelog(const char* log) { time_t tDate; struct tm* eventTime; time(&tDate);//得到系统当前时间 //将 ...
- Linux启动屏幕打印日志,linux启动时如何在屏幕上找到文本显示?这...
只有内核本身的输出才是dmesg. 另一个输出来自init进程及其启动的服务.在GNU / Linux上常用的init有很多不同的实现(其中一些不称为"init"),它们的行为可能 ...
- linux 硬盘报错日志,Linux内核I_O系统报错日志与硬盘故障对应关系
Linux 内核I/O 系统报错日志与硬盘故障对应关系 Jun04 2011Leave a Comment Written by chen 日志信息 故障现象描述 与硬盘关系 scsi1: ERROR ...
- linux查看文件写进程,Linux下如何知道文件被那个进程写
晚上朔海同学问:一个文件正在被进程写 我想查看这个进程 文件一直在增大 找不到谁在写 使用lsof也没找到 这个问题挺有普遍性的,解决方法应该很多,这里我给大家提个比较直观的方法. linux下每个文 ...
最新文章
- 反arp攻击软件_谈谈电子欺骗中的ARP欺骗
- (转载)VS2010/MFC编程入门之五十四(Ribbon界面开发:使用更多控件并为控件添加消息处理函数)...
- Python pip更换升级源
- java避免内存泄露_Java防止非静态内部类内存泄漏
- 一念心动,一生绵延——如何修复min pulse width?
- 前端JAVASCRIPT模块化开发
- Eclipse用法和技巧
- 11 Vue学习 headtop
- 快速判断一个数能否被 2 ,3 ,4 ,5, 7,9,11 整除
- 复现贪吃蛇程序——吃食物增加长度(最后一篇)
- Bat脚本学习之 常用测试脚本
- R语言求一行(列表、list)数据的平均数
- 报错 Tensorflow 2.0 Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
- Swagger API文档Responses中Object类型无法显示,求指引
- Docker创建私有仓库
- 基于python的微信公众号开发教程_基于python的微信公众号开发教程
- 使用域名访问远程jupyter_如何设置远程访问的Jupyter Notebook服务器-01(之预备知识:什么是端口号?)...
- 【实践】Stm32f10x_74Hc595倒计时实现实例
- 如何制定好的串口通信的指令通讯协议
- VMware Workstation Player虚拟机个人免费版
热门文章
- return的用法 java_Java中return用法.
- spring 事务传播机制和隔离级别
- 计算机网络未来的发展和趋势论文,计算机网络发展论文范文
- linux配置环境变量宏,在linux中配置环境变量(示例代码)
- Vue-cli下打包资源相对路径踩坑
- vue - webpack.dev.conf.js for FriendlyErrorsPlugin
- Android项目实战之高仿网易云音乐创建项目和配置
- OpenStack HA集群1-Galera Cluster for Mysql
- WP7 手机软件纪念 - 稍后读软件
- 通过HttpURLConnection模拟post表单提交