Skip to content
Snippets Groups Projects

Used the pthread_setcancelstate function to configure where the thread can be cancelled.

Merged Sergi Hernandez requested to merge pthread_cancel into master
1 file
+ 13
10
Compare changes
  • Side-by-side
  • Inline
+ 13
10
@@ -4,6 +4,7 @@
@@ -4,6 +4,7 @@
#include "comm.h"
#include "comm.h"
#include "commexceptions.h"
#include "commexceptions.h"
#include "ctime.h"
#include "ctime.h"
 
#include "pthread.h"
CComm::CComm(const std::string& comm_id)
CComm::CComm(const std::string& comm_id)
{
{
@@ -212,7 +213,9 @@ void CComm::close(void)
@@ -212,7 +213,9 @@ void CComm::close(void)
{
{
/* finish the thread */
/* finish the thread */
if(this->state==configured)
if(this->state==configured)
 
{
this->thread_server->kill_thread(this->comm_thread_id);
this->thread_server->kill_thread(this->comm_thread_id);
 
}
/* close the communication device */
/* close the communication device */
this->access_comm.try_enter();
this->access_comm.try_enter();
try{
try{
@@ -238,7 +241,9 @@ void *CComm::comm_thread(void *param)
@@ -238,7 +241,9 @@ void *CComm::comm_thread(void *param)
while(!end)
while(!end)
{
{
 
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
wait_result=comm_dev->hard_wait_comm_event();
wait_result=comm_dev->hard_wait_comm_event();
 
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);
try{
try{
comm_dev->access_comm.enter();
comm_dev->access_comm.enter();
if(wait_result==-1)
if(wait_result==-1)
@@ -256,6 +261,8 @@ void *CComm::comm_thread(void *param)
@@ -256,6 +261,8 @@ void *CComm::comm_thread(void *param)
}
}
comm_dev->access_comm.exit();
comm_dev->access_comm.exit();
}catch(CException &e){
}catch(CException &e){
 
comm_dev->access_comm.exit();
 
std::cout << e.what() << std::endl;
end=true;
end=true;
}
}
}
}
@@ -309,16 +316,12 @@ void CComm::on_error(void)
@@ -309,16 +316,12 @@ void CComm::on_error(void)
CComm::~CComm()
CComm::~CComm()
{
{
 
this->close();
/* delete the events */
/* delete the events */
if(this->rx_event_id.size()!=0)
this->event_server->delete_event(this->rx_event_id);
this->event_server->delete_event(this->rx_event_id);
this->rx_event_id="";
if(this->error_event_id.size()!=0)
this->event_server->delete_event(this->error_event_id);
{
this->error_event_id="";
this->event_server->delete_event(this->error_event_id);
}
/* delete the thread */
/* delete the thread */
if(this->comm_thread_id.size()!=0)
this->thread_server->delete_thread(this->comm_thread_id);
{
this->thread_server->delete_thread(this->comm_thread_id);
}
}
}
Loading