Linux C 为线程添加各自的日志文件
来源:互联网 发布:c语言 谭浩强 pdf 编辑:程序博客网 时间:2024/04/29 09:49
/* *============================================================================== * Filename : tsd.c * Description : * Author : RollStone (rs), jealdean@outlook.com * Created : 10/14/2014 15:14 * Copyright : 2007-2014 RollStone. All Rights Reserved. * Last_Change : 2014-10-14 15:53:05 * Version : 1.0 *============================================================================== */#include <malloc.h>#include <pthread.h>#include <stdio.h>/* The key used to associate a log file pointer with each thread. */static pthread_key_t thread_log_key;void write_to_thread_log (const char* message);/* Write MESSAGE to the log file for the current thread. */void close_thread_log (void* thread_log);/* Close the log file pointer THREAD_LOG. */void* thread_function (void* args);int main (){int i;pthread_t threads[5];/* Create a key to associate thread log file pointers in thread-specific data. Use close_thread_log to clean up the file pointers. */pthread_key_create (&thread_log_key, close_thread_log);/* Create threads to do the work. */for (i = 0; i < 5; ++i){pthread_create (&(threads[i]), NULL, thread_function, NULL);}/* Wait for all threads to finish. */for (i = 0; i < 5; ++i){pthread_join (threads[i], NULL);}return 0;}void write_to_thread_log (const char* message){FILE* thread_log = (FILE*) pthread_getspecific (thread_log_key);fprintf (thread_log, "%s\n", message);}void close_thread_log (void* thread_log){fclose ((FILE*) thread_log);}void* thread_function (void* args){char thread_log_filename[20];char wbuf[64]={0};FILE* thread_log;/* Generate the filename for this thread’s log file. */int sf=(int)pthread_self();sprintf (thread_log_filename, "thread%d.log", sf);sprintf (wbuf, "Thread %d starting", sf);/* Open the log file. */thread_log = fopen (thread_log_filename, "w");/* Store the file pointer in thread-specific data under thread_log_key. */pthread_setspecific (thread_log_key, thread_log);write_to_thread_log (wbuf);/* Do work here... */return NULL;}
0 0
- Linux C 为线程添加各自的日志文件
- Linux C写日志文件
- Linux文件目录和各自的作用示意图
- Linux的日志文件
- 为自己的日志添加背景
- oracle添加日志文件后状态为invalid
- linux 为文件添加、减少权限
- linux 为文件添加、减少权限
- c,c++,和java各自的优缺点
- 读取WEB-INF下的日志文件并添加到集合(windows和linux环境通用)
- 备份日志文件(增量添加各类日志、线程池实现)
- 导出选中的日志为excel文件
- qt5添加日志文件
- 三个线程,轮流打印10次各自的线程ID
- 为setup.py添加c:c++ 头文件或者库的搜索路径
- Linux添加日志审计
- 为opentack 日志添加行号
- apply方法的用途:为函数调用添加日志
- FFMPEG 硬件解码
- MFC中CFileDialog用法
- Adapter的用法
- NSUserDefaults
- Ubuntu查看内存信息
- Linux C 为线程添加各自的日志文件
- EasyUI----DataGrid行明细增删改操作
- 乱码的转换
- 找工作--笔试面试--准备7
- 成都特色小吃
- shell学习经典
- JavaScript跨域获得用户的IPv6地址
- Hadoop-2.4.1学习之使用Quorum Journal Manager的HDFS的高可用性(二)
- 有关android中的ADB默认端口被使用导致无法启动adb.exe的问题