TFCweb  1.0.4 $Rev: 483 $
TFC Primavera 2012: Nucli d'un servidor web
threading.cc
Veure la documentació d'aquest fitxer.
1 
11 /*
12  * Copyright (c) 2012 Toni Corvera
13  *
14  * This file is part of TFCWeb.
15  *
16  * TFCWeb is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * TFCWeb is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with TFCWeb. If not, see <http://www.gnu.org/licenses/>.
28  */
29 
30 #include <Log.h>
31 #include <ThreadPool.h>
32 
33 #include "comu.h"
34 
35 #include <iostream>
36 #include <memory>
37 #include <boost/lexical_cast.hpp>
38 
39 #include <unistd.h>
40 
41 using namespace tfc;
42 using namespace std;
43 
44 void my_callback() { // TODO:
45  cerr << "this is my callback" << endl;
46 }
47 
48 struct my_functor {
49  const int id_;
50  my_functor(int id) : id_(id) {}
51  virtual void operator()() {
52  using namespace boost;
53  const string msg = string("functor#")+lexical_cast<string>(id_)+
54  string("()() @ thread ")+
55  lexical_cast<string>(this_thread::get_id())+
56  string("\n");
57 
58  cerr << msg << flush;
59  boost::this_thread::sleep(boost::posix_time::seconds(1));
60  }
61 };
62 
63 
64 
65 void executa(const int nthreads, const int ntasques) {
66  cerr << "Creant pool..." << endl;
67  ThreadPool pool(nthreads);
68  cerr << "Programant..." << endl;
69  for (int i=0; i<ntasques; ++i) {
70  pool.programa(my_functor(i));
71  }
72  cerr << "Programades; 1s i abortem les que no s'hagin completat" << endl;
73  sleep(1);
74  pool.termina();
75 }
76 
77 int main(int argc, char ** argv, char ** envp) {
79 
80  log() << "Executant test amb 100 threads i 1000 tasques" << commit;
81  executa(100,1000);
82  return 0;
83 }
84 
85 // vim:set ts=4 et ai: //