Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This Thread class that i inherit from it and implement function run in child classes
---
C#
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;
}

and this handleRequest class inherit from Ithread and i call this function in its run
C#
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
C#
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());
   }




the problem if i kill Thread read if client close session the all thread kill and app stop
Posted
Updated 10-Aug-14 23:48pm
v2

1 solution

XML
#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);
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900