General IRI utilities
test_threads.cpp
1 // Copyright (C) 2009-2010 Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
2 // Author Sergi Hernandez (shernand@iri.upc.edu)
3 // All rights reserved.
4 //
5 // This file is part of iriutils
6 // iriutils is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 #include "thread.h"
20 #include "threadexceptions.h"
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <iostream>
24 
26 
33 void *my_thread_function(void *param)
34 {
35  int i=0;
36 
37  for(i=0;i<=2;i++)
38  {
39  std::cout << "loop number: " << i << std::endl;
40  sleep(1);
41  }
42  pthread_exit(NULL);
43 }
44 
46 
54 void *my_thread_function2(void *param)
55 {
56  int i=0;
57 
58  while(1)
59  {
60  std::cout << "another loop number: " << i << std::endl;
61  i++;
62  sleep(1);
63  }
64  pthread_exit(NULL);
65 }
66 
68 
75 void *my_thread_function3(void *param)
76 {
77  int i=0;
78  int num_iter=*((int *)param);
79 
80  for(i=0;i<=num_iter;i++)
81  {
82  std::cout << "user defined loop number: " << i << std::endl;
83  sleep(1);
84  }
85 
86  pthread_exit(NULL);
87 }
88 
161 int main(int argc, char *argv[])
162 {
163  int num_iter=3;
164  std::string thread_id;
165  CThread Thread("test_thread"),*Thread2;
166 
167  // get the thread identifier
168  thread_id=Thread.get_id();
169  std::cout << "Thread identifier: " << thread_id << std::endl;
170  // test with the first function
171  // the expected state is detachhed (3)
172  std::cout << "Thread state: " << Thread.get_state() << std::endl;
173  Thread.attach(my_thread_function,NULL);
174  // the expected state is attached (0)
175  std::cout << "Thread state: " << Thread.get_state() << std::endl;
176  Thread.start();
177  // the expected state is active (2) or starting (1)
178  std::cout << "Thread state: " << Thread.get_state() << std::endl;
179  // wait until the thread executes all the iterations
180  Thread.end();
181  // the expected state is attached (0)
182  std::cout << "Thread state: " << Thread.get_state() << std::endl;
183  Thread.detach();
184  // the expected state is detached (3)
185  std::cout << "Thread state: " << Thread.get_state() << std::endl;
186 
187  // test with the second function
188  Thread.attach(my_thread_function2,NULL);
189  // the expected state is attached (0)
190  std::cout << "Thread state: " << Thread.get_state() << std::endl;
191  Thread.start();
192  // the expected state is active (2) or starting (1)
193  std::cout << "Thread state: " << Thread.get_state() << std::endl;
194  // wait for the thread to execute some loops (15 aprox)
195  sleep(3);
196  // cancel the thread immediatelly
197  Thread.kill();
198  // the expected state is attached (0)
199  std::cout << "Thread state: " << Thread.get_state() << std::endl;
200 
201  // test with the thirth function
202  Thread.attach(my_thread_function3,&num_iter);
203  // the expected state is attached (0)
204  std::cout << "Thread state: " << Thread.get_state() << std::endl;
205  Thread.start();
206  // the expected state is active (2) or starting (1)
207  std::cout << "Thread state: " << Thread.get_state() << std::endl;
208  // wait until the trhead executes num_iter iterations
209  Thread.end();
210  // the expected state is attached (0)
211  std::cout << "Thread state: " << Thread.get_state() << std::endl;
212  Thread.detach();
213  // the expected state is detached (3)
214  std::cout << "Thread state: " << Thread.get_state() << std::endl;
215 
216  // test thread exceptions
217  try{
218  Thread.attach(NULL,NULL);
219  }catch(CThreadException &e){
220  std::cout << e.what() << std::endl;
221  }
222  try{
223  Thread.start();
224  }catch(CThreadException &e){
225  std::cout << e.what() << std::endl;
226  }
227  try{
228  Thread2=new CThread("");
229  delete Thread2;
230  }catch(CThreadException &e){
231  std::cout << e.what() << std::endl;
232  }
233 }
virtual const std::string & what(void)
Function to get the error message.
Definition: exceptions.cpp:33
Implementation of a parallel thread of execution.
Definition: thread.h:76
std::string get_id(void)
Function to get the thread identifier.
Definition: thread.cpp:161
Thread exception class.