如何在没有杀父母的情况下杀死子线程? [英] How to Kill child thread without kill parent ?
本文介绍了如何在没有杀父母的情况下杀死子线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从它继承的Thread类和实现在子类中运行的函数
---
This Thread class that i inherit from it and implement function run in child classes
---
IThread::IThread()
{
m_tid = 0;
m_running = -1;
m_detached = -1;
}
/*static void* runThread(void* arg)
{
}*/
IThread::~IThread()
{
if (m_running == 1 && m_detached == 0) {
pthread_detach(m_tid);
}
if (m_running == 1) {
printf("\n from thread %lu \n",m_tid);
pthread_cancel(m_tid);
}
}
int IThread::start()
{
int result = pthread_create(&m_tid, NULL, runThread, this);
if (result == 0) {
m_running = 1;
}
return result;
}
int IThread::join()
{
int result = -1;
if (m_running == 1) {
result = pthread_join(m_tid, NULL);
if (result == 0) {
m_detached = 1;
}
}
return result;
}
int IThread::detach()
{
int result = -1;
if (m_running == 1 && m_detached == 0) {
result = pthread_detach(m_tid);
if (result == 0) {
m_detached = 1;
}
}
return result;
}
pthread_t IThread::self() {
return m_tid;
}
void* IThread::run()
{
printf("DFDF");
return 0;
}
这个handleRequest类继承自Ithread,我在其运行中调用此函数
and this handleRequest class inherit from Ithread and i call this function in its run
void HandleRequest::AcceptConnection()
{
struct sockaddr_in client;
unsigned int len;int s;
;//= new HandleRequest();
CircularResponse * r;// = new CircularResponse();
len = sizeof(client);
while(true)
{
s = accept(Socket,(struct sockaddr*)&client,&len);
if( s > -1)
{
r = new CircularResponse(); // create response Thread for each client
printf("\n other client port %i\n",client.sin_port);
HandleResponse * re = new HandleResponse();
re->;set_Client(s);
r->addClient(re);
r->start();
printf("\n fn\n");
}
else
{
printf("\n listing for connect \n");
}
}
}
和这个函数线程为每个回复运行
and this function thread that run for every response
while(true)
{
if(!qclient->empty()&&!wait)
{
current = qclient->front();
current->start();
qclient->pop();
h = current->check_connect();
if(h==-1)
{
current->detach();
delete current;
printf("\n running %i \n",current->m_running);
// pthread_cancel(current->m_tid);
pthread_kill(current->m_tid,SIGKILL);
printf("running %i \n",current->m_running);
printf("\n here I kill thread %lu \n",current->self());
break;
}
// delete current;
// continue;
// }
qclient->push(current);
current->join();
printf("\n client connected %i \n",(int)qclient->size());
}
问题如果我杀死线程读取客户端关闭会话所有线程kill和app stop
the problem if i kill Thread read if client close session the all thread kill and app stop
推荐答案
#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t pID = fork();
/* child process */
if (pID == 0)
{
printf ("\nReady to murder the parent!\n");
kill (getppid(), 9);
}
/* parent process */
else
{
printf ("\nLet the child execute first!\n");
wait(0);
}
}
这篇关于如何在没有杀父母的情况下杀死子线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文