File: ThreadedFind.cc

package info (click to toggle)
exactimage 1.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,040 kB
  • sloc: cpp: 35,940; ansic: 1,952; xml: 1,447; makefile: 338; perl: 138; sh: 110; python: 45; php: 37; ruby: 12
file content (67 lines) | stat: -rw-r--r-- 1,286 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#include <vector>
#include <iostream>
#include <sstream>

#include "DirIterator.hh"
#include "Threads.hh"

Utility::Threads::Mutex big_lock;

class Scan : public Utility::Threads::Thread
{
private:
  std::string s;
public:
  Scan (const std::string i_s) {
    // std::cout << "Creating thread for " << i_s << std::endl;
    s = i_s;
    Create (0);
  }
  
protected:
  
  void* main (void*)
  {
    Utility::DirList dir (s);
    Utility::DirList::Iterator it, it_end;
    std::vector<Scan*> threads;
    
    it = dir.Begin ();
    it_end = dir.End ();
    
    while  (it != it_end) {
      big_lock.Lock();
      std::cout << s + '/' + *it << std::endl;
      big_lock.Unlock();
      
      if (it.Type().IsDirectory()) {
	Scan* new_thread = new Scan (s + '/' + *it);
	threads.push_back(new_thread);
      }
      ++ it;
    }
    
    // join all threads ...
    while (threads.size()>0) {
      Scan* thread = threads.back();
      // std::cout << "Waiting for thread " << thread->s << std::endl;
      thread->Join();
      delete thread;
      threads.pop_back();
    }
    
    return 0;
  }
};

int main (int argc, char* argv[])
{
  std::string start ("/home/rene");
  if (argc > 1)
    start = argv[1];
  
  Scan s (start);
  s.Join();
  //std::cout << "EOP" << std::endl;
}