下述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++多线程操作文件&输出加锁的更多相关文章

  1. Linux下多线程复制文件(C)

    Linux下实现多线程文件复制,使用<pthread.h>提供的函数: int pthread_create(pthread_t *thread,const pthread_attr_t ...

  2. linux exec操作文件描述符

    linux每一个打开文件都会关联一个文件描述符,需要的时候我们可以使用exec命令指定一个大于3的数字作为文件 linux默认文件描述符 每打开一个shell就会打开默认的三个文件描述符描0,1,2, ...

  3. Linux目录结构以及文件操作

    Linux目录结构 UNIX 是以目录为主的,Linux 也继承了这一优良特性. Linux 是以树形目录结构的形式来构建整个系统的,可以理解为树形目录是一个用户可操作系统的骨架.虽然本质上无论是目录 ...

  4. Linux基础命令之文件和目录操作(二)

    . find 用于查找目录下的文件,也可以调用其他命令使用 find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression] fi ...

  5. Linux C高级编程——文件操作之系统调用

    Linux C高级编程文件操作之系统调用 宗旨:技术的学习是有限的,分享的精神是无限的.           库函数是一些完毕特定功能的函数.一般由某个标准组织制作公布,并形成一定的标准.使用库函数编 ...

  6. linux c编程:文件的操作

    在Linux系统中,系统是通过inode来获得这个文件的信息.在Linux系统中,inode的信息都是封装在stat这个结构体中.可以通过man 2 stat来查看stat的具体结构.从中可以看到包含 ...

  7. 【转】【整理】将Linux脚本中的正常输出,警告,错误等信息输出到文件中

     本文来自:http://blog.csdn.net/woshinia/article/details/18040063   很早以前  编译的时候 就在用 2>&1,但是一直没有生成一 ...

  8. ubuntu chmod 无法更改 文件夹权限 系统提示“不允许的操作 2、linux 如何修改只读文件 3、ubuntu安装

    1.ubuntu chmod 无法更改 文件夹权限 系统提示“不允许的操作 答案:需要超级用户权限 sudo 2.linux 如何修改只读文件 答案:可以使用chmod命令,为改文件提供其他的权限.u ...

  9. java文件操作(输出目录、查看磁盘符)

    问题描述:     java操作文件,所有硬盘中所有文件路径 问题解决:     (1)查看所有磁盘文件 注:     如上所示,使用接口 File.listRoots()可以返回所有磁盘文件,通过f ...

随机推荐

  1. ●洛谷P2606 [ZJOI2010]排列计数

    题链: https://www.luogu.org/problemnew/show/P2606题解: 组合数(DP),Lucas定理 首先应该容易看出,这个排列其实是一个小顶堆. 然后我们可以考虑dp ...

  2. Codeforces Round #430 A. Kirill And The Game

    Kirill plays a new computer game. He came to the potion store where he can buy any potion. Each poti ...

  3. SQL 收缩数据库日志的几种办法 (2005与2008 略有区别)

    在SQL Server 2000/2005中可以快速压缩日志log文件,通过SQL, 方法一: ---DBTEST 为数据库名,顺序最好别乱.注意:要先截断再清空,最后收缩! backup log D ...

  4. Java8——快速入门手册(学习笔记)

    github博文传送门 Java8特性学习笔记 Java8中新增了许多的新特性,在这里本人研究学习了几个较为常用的特性,在这里与大家进行分享.(这里推荐深入理解Java 8用于理解基础知识)本文分为以 ...

  5. 40道Java初中级算法面试题

    [程序1]   题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析:   兔子的规律为数 ...

  6. 讨论过后而引发对EF 6.x和EF Core查询缓存的思考

    前言 最近将RabbitMQ正式封装引入到.NET Core 2.0项目当中,之前从未接触过是个高大上的东东跟着老大学习中,其中收获不少,本打算再看看RabbitMQ有时间写写,回来后和何镇汐大哥探讨 ...

  7. Python练习之pillow

    此系列意在记录于一些有趣的程序及对其的总结. 问题来源: https://github.com/Yixiaohan/show-me-the-code https://github.com/HT524/ ...

  8. 再见,segmentfault

    再见,segmentfault 太多的Bug,对segmentfault已经爱不起了. 重回博客园~

  9. 介绍Docker容器

    容器是 Docker 又一核心概念. 简单的说,容器是独立运行的一个或一组应用,以及它们的运行态环境.对应的,虚拟机可以理解为模拟运行的一整套操作系统(提供了运行态环境和其他系统环境)和跑在上面的应用 ...

  10. C++后台实践:古老的CGI与Web开发

    本文写给C/C++程序猿,也适合其他对历史感兴趣的程序猿 ============================================= 谈到web开发,大家首先想到的PHP.JavaEE ...