General IRI utilities
test_threadserver.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 "threadserver.h"
20 #include "threadexceptions.h"
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <iostream>
24 
28 const std::string thread1="thread_1";
32 const std::string thread2="thread_2";
33 
37 const std::string thread3="invalid_thread";
38 
45 void *my_thread_function1(void *param)
46 {
47  int i=0;
48 
49  while(1)
50  {
51  std::cout << "thread 1 loop " << i << std::endl;
52  i++;
53  sleep(1);
54  }
55  pthread_exit(NULL);
56 }
57 
64 void *my_thread_function2(void *param)
65 {
66  int i=0;
67 
68  for(i=0;i<15;i++)
69  {
70  std::cout << "thread 2 loop " << i << std::endl;
71  sleep(1);
72  }
73 
74  pthread_exit(NULL);
75 }
76 
141 int main(int argc, char *argv[])
142 {
143  CThreadServer *thread_server;
144 
145  thread_server=CThreadServer::instance();
146 
147  thread_server->create_thread(thread1);
148  thread_server->create_thread(thread2);
149 
150  thread_server->attach_thread(thread1,my_thread_function1,NULL);
151  thread_server->attach_thread(thread2,my_thread_function2,NULL);
152 
153  thread_server->start_thread(thread1);
154  sleep(2);
155  thread_server->start_thread(thread2);
156  thread_server->end_thread(thread2);
157  thread_server->kill_thread(thread1);
158 
159  thread_server->detach_thread(thread1);
160  try{
161  thread_server->start_thread(thread1);
162  }catch(CThreadException &e){
163  std::cout << e.what() << std::endl;
164  }
165 
166  thread_server->delete_thread(thread1);
167  thread_server->delete_thread(thread2);
168 
169  try{
170  thread_server->attach_thread(thread3,my_thread_function2,NULL);
171  }catch(CThreadServerException &e){
172  std::cout << e.what() << std::endl;
173  }
174 }
175 
Thread server exception class.
void create_thread(const std::string &thread_id)
Function to create a new thread.
void start_thread(const std::string &thread_id)
Function to start a thread.
virtual const std::string & what(void)
Function to get the error message.
Definition: exceptions.cpp:33
Thread exception class.
void kill_thread(const std::string &thread_id)
Function to immediately terminate a thread.
void end_thread(const std::string &thread_id)
Function to request the termination of a thread.
void delete_thread(const std::string &thread_id)
Function to delete an existing thread.
void attach_thread(const std::string &thread_id, void *(*user_thread_function)(void *param), void *param)
Function to attach a function to a thread.
void detach_thread(const std::string &thread_id)
Function to detach a function from a thread.
Global thread server.
Definition: threadserver.h:57
static CThreadServer * instance(void)
Function to get a reference to the unique instance.