This is an example on how to use threads
This example shows how to use the CThread class. In this example three different thread functions are attached to a single thread:
- The first function executes a loop a finite number of times and then exits. So the end() function can be used to wait for the thread to end.
- The second function executes an infinite loop that is only interrupted by the call to the kill function which immediately terminates the thead.
- The last function repeats the loop as many times as indicated by the thread function parameter. The end function can be used to wait for the thread to end.
Notice that the sequence of functions called is always the same: first a function is attached (attach()) and then the thread is started by calling the start() function. If the thread ends by its own, the end() function can be used, otherwise, it is necessary to use the kill() function to terminate the thread.
After terminating the execution of the thread, a new function can be attached or the previous one restarted. Notice that it is not required to use the detach() function to remove the previous function before attaching a new one.
In this case the name provided to the thread is just informative, but when the CThreadServer class is used to handle multiple threads, this name is used to identify thre desired thread, so each thread must have a different name.
The output of this example should be something like this:
* Thread identifier: test_thread
* Thread state: 3
* Thread state: 0
* Thread state: 1
* loop number: 0
* loop number: 1
* loop number: 2
* Thread state: 0
* Thread state: 3
* Thread state: 0
* Thread state: 1
* another loop number: 0
* another loop number: 1
* another loop number: 2
* Thread state: 0
* Thread state: 0
* Thread state: 1
* user defined loop number: 0
* user defined loop number: 1
* user defined loop number: 2
* user defined loop number: 3
* Thread state: 0
* Thread state: 3
* [Exception caught] - [CThread class] - Invalid thread function - test_thread
* [Exception caught] - [CThread class] - The thread has not been attached to a function - test_thread
* [Exception caught] - [CThread class] - Invalid thread id - NULL pointer
*
Several error may be thrown by the CThread class:
- An invalid thread function is given to the attach() function.
- A thread is started without any valid thread function.
- A thread is given an invalid identifier at construction time.
#include "thread.h"
#include "threadexceptions.h"
#include <stdio.h>
#include <unistd.h>
#include <iostream>
void *my_thread_function(void *param)
{
int i=0;
for(i=0;i<=2;i++)
{
std::cout << "loop number: " << i << std::endl;
sleep(1);
}
pthread_exit(NULL);
}
void *my_thread_function2(void *param)
{
int i=0;
while(1)
{
std::cout << "another loop number: " << i << std::endl;
i++;
sleep(1);
}
pthread_exit(NULL);
}
void *my_thread_function3(void *param)
{
int i=0;
int num_iter=*((int *)param);
for(i=0;i<=num_iter;i++)
{
std::cout << "user defined loop number: " << i << std::endl;
sleep(1);
}
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
int num_iter=3;
std::string thread_id;
CThread Thread(
"test_thread"),*Thread2;
std::cout << "Thread identifier: " << thread_id << std::endl;
std::cout << "Thread state: " << Thread.get_state() << std::endl;
Thread.attach(my_thread_function,NULL);
std::cout << "Thread state: " << Thread.get_state() << std::endl;
Thread.start();
std::cout << "Thread state: " << Thread.get_state() << std::endl;
Thread.end();
std::cout << "Thread state: " << Thread.get_state() << std::endl;
Thread.detach();
std::cout << "Thread state: " << Thread.get_state() << std::endl;
Thread.attach(my_thread_function2,NULL);
std::cout << "Thread state: " << Thread.get_state() << std::endl;
Thread.start();
std::cout << "Thread state: " << Thread.get_state() << std::endl;
sleep(3);
Thread.kill();
std::cout << "Thread state: " << Thread.get_state() << std::endl;
Thread.attach(my_thread_function3,&num_iter);
std::cout << "Thread state: " << Thread.get_state() << std::endl;
Thread.start();
std::cout << "Thread state: " << Thread.get_state() << std::endl;
Thread.end();
std::cout << "Thread state: " << Thread.get_state() << std::endl;
Thread.detach();
std::cout << "Thread state: " << Thread.get_state() << std::endl;
try{
Thread.attach(NULL,NULL);
std::cout << e.
what() << std::endl;
}
try{
Thread.start();
std::cout << e.
what() << std::endl;
}
try{
delete Thread2;
std::cout << e.
what() << std::endl;
}
}