linux C++多线程操作文件&输出加锁
下述demo将指定目录下文件存入vector,然后一分为二交给两个线程(threadTask1,threadTask2)去分别处理,对输出函数printDirent加锁防止紊乱。
#include <iostream>
#include <string>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <list>
#include <numeric>
#include <algorithm>
#include <vector> static pthread_mutex_t testlock;
using namespace std;
void *threadTask1(void *ptr);
void *threadTask2(void *ptr);
typedef vector<dirent> VECTORDIRENT;
VECTORDIRENT listDirent;
VECTORDIRENT::iterator iter;
void printStr(string buff);
void printDirent(struct dirent ptrTest); void *threadTask1(void *ptr)
{
int vectorI=0;
int vectorLen = listDirent.size()/2;
int count=0;
for(;vectorI<vectorLen;vectorI++)
{
printDirent(listDirent[vectorI]);
count++;
}
} void *threadTask2(void *ptr)
{
int vectorI=listDirent.size()/2;
int vectorLen = listDirent.size();
int count=0;
for(;vectorI<vectorLen;vectorI++)
{
printDirent(listDirent[vectorI]);
count++;
}
} void printDirent(struct dirent ptrTest){
pthread_mutex_lock(&testlock);
cout<<ptrTest.d_name<<endl;
pthread_mutex_unlock(&testlock); } int main(void)
{
DIR *dirTest;
struct dirent *ptrTest;
string testFolderPathName = "/home/undoner/";
dirTest = opendir(testFolderPathName.data()); ///open the dir
int fileCount = 0;
while((ptrTest = readdir(dirTest)) != NULL) ///read the list of this dir
{
if(ptrTest->d_type==8){
listDirent.push_back(*ptrTest);
}
}
cout<<listDirent.size()<< endl;
cout<<"listDirent.begin()--- listDirent.end():"<<endl;
for (iter = listDirent.begin(); iter != listDirent.end(); ++iter) {
cout << iter->d_name << " ";
}
pthread_t thread1,thread2;
int ret1,ret2;
ret1=pthread_create(&thread1,NULL,threadTask1,NULL);
ret2=pthread_create(&thread2,NULL,threadTask2,NULL);
//ret3=pthread_create(&thread3,NULL,thread,NULL);
if(ret1!=0){
printf ("Create pthread error!\n");
exit (1);
}
if(ret2!=0){
printf ("Create pthread error!\n");
exit (1);
}
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
return 0;
}
linux C++多线程操作文件&输出加锁的更多相关文章
- Linux下多线程复制文件(C)
Linux下实现多线程文件复制,使用<pthread.h>提供的函数: int pthread_create(pthread_t *thread,const pthread_attr_t ...
- linux exec操作文件描述符
linux每一个打开文件都会关联一个文件描述符,需要的时候我们可以使用exec命令指定一个大于3的数字作为文件 linux默认文件描述符 每打开一个shell就会打开默认的三个文件描述符描0,1,2, ...
- Linux目录结构以及文件操作
Linux目录结构 UNIX 是以目录为主的,Linux 也继承了这一优良特性. Linux 是以树形目录结构的形式来构建整个系统的,可以理解为树形目录是一个用户可操作系统的骨架.虽然本质上无论是目录 ...
- Linux基础命令之文件和目录操作(二)
. find 用于查找目录下的文件,也可以调用其他命令使用 find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression] fi ...
- Linux C高级编程——文件操作之系统调用
Linux C高级编程文件操作之系统调用 宗旨:技术的学习是有限的,分享的精神是无限的. 库函数是一些完毕特定功能的函数.一般由某个标准组织制作公布,并形成一定的标准.使用库函数编 ...
- linux c编程:文件的操作
在Linux系统中,系统是通过inode来获得这个文件的信息.在Linux系统中,inode的信息都是封装在stat这个结构体中.可以通过man 2 stat来查看stat的具体结构.从中可以看到包含 ...
- 【转】【整理】将Linux脚本中的正常输出,警告,错误等信息输出到文件中
本文来自:http://blog.csdn.net/woshinia/article/details/18040063 很早以前 编译的时候 就在用 2>&1,但是一直没有生成一 ...
- ubuntu chmod 无法更改 文件夹权限 系统提示“不允许的操作 2、linux 如何修改只读文件 3、ubuntu安装
1.ubuntu chmod 无法更改 文件夹权限 系统提示“不允许的操作 答案:需要超级用户权限 sudo 2.linux 如何修改只读文件 答案:可以使用chmod命令,为改文件提供其他的权限.u ...
- java文件操作(输出目录、查看磁盘符)
问题描述: java操作文件,所有硬盘中所有文件路径 问题解决: (1)查看所有磁盘文件 注: 如上所示,使用接口 File.listRoots()可以返回所有磁盘文件,通过f ...
随机推荐
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined)
C题卡了好久,A掉C题之后看到自己已经排在好后面说实话有点绝望,最后又过了两题,总算稳住了. AC:ABCDE Rank:191 Rating:2156+37->2193 A.Oath of t ...
- [USACO Dec06]产奶的模式
Description 农夫约翰发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠.我们称之为一个“模式”. 约翰的牛奶按质量可以被 ...
- PAT甲级真题打卡:1001.A+B Format
题目: Calculate a + b and output the sum in standard format -- that is, the digits must be separated i ...
- CentOS7.2安装mysql5.6
1.卸载系统自带的Mariadb [root@localhost~]# rpm -qa|grep mariadb //查询出已安装的mariadb [root@localhost~]# rpm -e ...
- jquery easyui combobox 高度自适应
data-options="required:true,editable:false,panelHeight:'auto'" 加上panelHeight:'auto'即可 列合并 ...
- 【问底】徐汉彬:亿级Web系统搭建——单机到分布式集群
http://www.csdn.net/article/2014-11-06/2822529/3 大规模流量的网站架构,从来都是慢慢"成长"而来.而这个过程中,会遇到很多问题,在不 ...
- 在vue中操作DOM--this.$nextTick()
虽然 Vue.js 通常鼓励开发人员沿着"数据驱动"的方式思考,避免直接接触 DOM,但是有时我们确实要这么做.比如一个新闻滚动的列表项.如果在这里需要操作dom, 应该是等待 V ...
- Lintcode393 Best Time to Buy and Sell Stock IV solution 题解
[题目描述] Say you have an array for which the i th element is the price of a given stock on day i. Desi ...
- Kafka生产者-向Kafka中写入数据
(1)生产者概览 (1)不同的应用场景对消息有不同的需求,即是否允许消息丢失.重复.延迟以及吞吐量的要求.不同场景对Kafka生产者的API使用和配置会有直接的影响. 例子1:信用卡事务处理系统,不允 ...
- ACM Bee
In Africa there is a very special species of bee. Every year, the female bees of such species give b ...