通过线程写文件的例子
来源:互联网 发布:rds数据库 编辑:程序博客网 时间:2024/06/06 22:19
两个线程,分别写两个文件。
#include<iostream>#include<fstream>#include<pthread.h>#include <stdlib.h>#include <string>using namespace std;pthread_t tid1;pthread_t tid2;typedef struct a_param{ ofstream fd; string filename; string data;}PARAM;void* writefile(void *arg){ PARAM *tmp = (PARAM *)arg; tmp->fd.open(tmp->filename.c_str()); if(!tmp->fd) { cout<<"unable to open :"<<tmp->filename<<endl; exit(-1); } tmp->fd<<tmp->data; tmp->fd.close(); pthread_exit(0);}int main(){ string file1="/home/app/ShallPractice/file1.txt"; string file2="/home/app/ShallPractice/file2.txt";PARAM redata1; redata1.filename =file1; redata1.data ="I am the file1 \n"; PARAM redata2; redata2.filename =file2; redata2.data ="I am the file2 \n";int t = pthread_create(&tid1,NULL,writefile,&redata1);if(t!=0) { cout<<"Thread1 create error!"<<endl; exit(-1); }t = pthread_create(&tid2,NULL,writefile,&redata2); if(t!=0) { cout<<"Thread2 create error!"<<endl; exit(-1); } pthread_join(tid1, NULL); pthread_join(tid2, NULL); return 0;}改掉路径名,重新编译即可运行。
0 0
- 通过线程写文件的例子
- 一个资源写到文件的例子
- ria 里面的写文件例子
- 一个MATLAB 写xml 文件的例子
- 通过一个有趣的例子说明线程的工作机制
- c++ 文件写例子
- 一个通过线程更新UI的例子(利用message)
- 通过线程池使用多线程并发:ThreadPoolTaskExecutor 的应用例子
- JSP中读文件和写文件的例子
- JSP中读文件和写文件的例子
- JSP中读文件和写文件的例子
- JSP中读文件和写文件的例子
- JSP中读文件和写文件的例子
- JSP中读文件和写文件的例子
- JSP中读文件和写文件的例子
- 自己写的一个多文件的makefile的例子
- 通过一个文件拷贝的例子了解二进制文件的读写
- 写的一个删除文件的小例子
- 员工的激励与自我激励 - 项目管理系列文章
- PhoneGap build生成apk,打开后报Application Error等等
- shell 中if 语句的使用
- php 代码优化,编写更高效的代码
- 安卓报java.lang.NoClassDefFoundError
- 通过线程写文件的例子
- 笔记:编译和运行Bundler (在cygwin下)
- C#中char[]与string之间的转换
- windows 系统 ---窗口篇
- c++实现无用产生式的消除
- #define用法总结
- listView嵌套button或者其它clickable控件时会出现listView的onitemClick事件不响应的问题
- 测试Fragment(跳转回传onActivityResult问题)
- EF6增改删等常用基类