下述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. ●POJ 1990 MooFest

    题链: http://poj.org/problem?id=1990 题解: 树状数组 把牛们按x坐标从小到大排序,依次考虑每头牛对左边和对右边的贡献. 对左边的贡献:从左向右枚举牛,计算以当前牛的声 ...

  2. 【网络流】【BZOJ1001】狼抓兔子

    继续网络流的学习.... 题意简析:就是给你张图,叫你求最小割. 解题思路:最小割=最大流,按题意见图跑一次就好了. 附代码: #include<cstdio> #include<i ...

  3. [BZOJ]4200: [Noi2015]小园丁与老司机

    Time Limit: 20 Sec  Memory Limit: 512 MBSec  Special Judge Description 小园丁 Mr. S 负责看管一片田野,田野可以看作一个二维 ...

  4. 12563 Jin Ge Jin Qu hao

    • Don’t sing a song more than once (including Jin Ge Jin Qu). • For each song of length t, either si ...

  5. ●BZOJ 3545 [ONTAK2010]Peaks(离线)

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3545 http://www.lydsy.com/JudgeOnline/problem.ph ...

  6. 【Halum操作-UVA 11478】

    ·英文题,述大意:      输入有向图一个(什么边的端点啊,边权啊).每次可以选择一个节点和一个整数,然后把这个结点的出边边权加上该整数,入边边权减去该整数,目标:使得所有边的最小值非负且尽量大. ...

  7. hdu 1255 覆盖的面积(求覆盖至少两次以上的面积)

    了校赛,还有什么途径可以申请加入ACM校队?  覆盖的面积 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  8. salt基本使用之一(1)

    1.帮助文档 以cmd函数为例子说下salt中的一些帮助信息 # 查看指定modules用法 salt '*' sys.doc cmd # 查看所有的modules列表 salt '*' sys.li ...

  9. Linux 脚本为什么会有#!

    我们在Linux系统终端编辑脚本时第一行通常这么写: #!/usr/bin/env python3 为什么要写这一行,起什么作用? 我们先看看不写这一行会出现什么?那么就会报下面的错误. #!/usr ...

  10. python3全栈开发-并发编程,多进程的基本操作

    一 .multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大部分情况需要使用多进程. ...