Thread: Terminating secondary thread from another thread

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    3

    Terminating secondary thread from another thread

    Hi everyone, I'm new to this forum. If my qurestion is in wrong place please correct me.
    I'm currently learning about the multithread, and TCP socket. But I was wondering why my code have problem. I search over google still cant fix my problem. Hope anyone can direct me into the right. Thanks

    Basically, this is a TCP client program which do simple msg tranfsfer. When the connection establish, the thread "new_thr" will create another 2 thread which are "send_thr" and "recv_thr". One is to send message to server and another one is to receive message from server. At final, what I want is when either receive or user input an "Q" or "q", the whole process will terminated. So currently I m trying to use thread_cancel to terminate another thread.
    Here is my code


    PHP Code:
    [CODE]/* threadclient.c */
    #include <pthread.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>

    void *cthr_do();
    void *cthr_send();
    void *cthr_recv();


    int main()
    {    
        
    int k;
        
    pthread_t new_thr;
        
    int check pthread_create(&new_thrNULLcthr_doNULL );
        
        
        if(
    check!=0)
        
    printf("The thread cannot be created!");
        
        
    pthread_join (new_thrNULL);
        
        
    printf("The whole process terminated\n");
        return 
    0;
    }

    void *cthr_do()
    {    
        
    printf("thread run sucessful\n");

            
    int sockbytes_receivedconnectedsr;  
            
    char send_data[1024],recv_data[1024];
            
    struct hostent *host;
            
    struct sockaddr_in6 server_addr;  

            
    host gethostbyname("::1");

            if ((
    sock socket(AF_INET6SOCK_STREAM0)) == -1) {
                
    perror("Socket");
                exit(
    1);
            }

            
    server_addr.sin6_family AF_INET6;     
            
    server_addr.sin6_port htons(5000);   
            
    server_addr.sin6_addrin6addr_any/*((struct in_addr *)host->h_addr)*/;
            
    /*bzero(&(server_addr.sin_zero),8);*/
        /*memset(&(server_addr.sin_zero),0,8);*/
            
            
    connected connect(sock, (struct sockaddr *)&server_addrsizeof(server_addr));
             
        if (
    connected== -1
            {
                
    perror("Connect");
                exit(
    1);
            }

        
    printf("The connection is established.\n");    
        
        
    pthread_t send_thrrecv_thr;
                  
                  
    int chk_send pthread_create(&send_thrNULLcthr_send, (void *)sock);
                  if(
    chk_send!=0)
                  
    printf("The send message thread cannot be created!");
                  
                  
    int chk_recv pthread_create(&recv_thrNULLcthr_recv, (void *)sock);              
                  if(
    chk_recv!=0)
                  
    printf("The receive message thread cannot be created!");
                  
                  while (
    1)
                  {
                  
    pthread_join (send_thrNULL);
                  
    pthread_join (recv_thrNULL);
                  
                  if (
    && r)
                  break;
                  }
                  
        
    close(sock);
        
    printf("closed socket\n");              
    }


    void *cthr_send(void *arg)
    {          
              
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLENULL);
              
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUSNULL);
              

              
    char send_data [1024], recv_data[1024]; 
              while (
    1)
              {
                  
    printf("\n SEND (q or Q to quit) : ");
                  
    gets(send_data)/*,1024,stdin)*/;
                  
                  if (
    strcmp(send_data "q") == || strcmp(send_data "Q") == 0)
                  {
                    
    send((int) argsend_data,strlen(send_data), 0);
                    
    /*close ((int) arg);*/
                    
    break;            
                  }
                   
                  else
                     
    send((int) argsend_data,strlen(send_data), 0);
                  }  
                  
                  if ( 
    pthread_cancel(recv_thr) == -
                  {
                  
    perror("pthread_cancel failed");
                  exit(
    3);
                  }  
    //              if (pthread_cancel(recv_thr)!= 0)
    //              perror ("Thread cancelation ");              ;              
    }

    void *cthr_recv(void *arg)
    {
             
                  
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLENULL);
                  
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUSNULL);
                  
                  
    char send_data [1024] , recv_data[1024]; 
                  
    int bytes_received;
                  
                  
                  while (
    1)
                  {
                  
    bytes_received recvfrom((int) argrecv_data1024-10NULLNULL);
                  
    /*recv((int) arg,recv_data,1024,0);*/
                 
                  
    if (bytes_received == -1)
                {
                  
    /*
                   * Socket in error state
                   */
                  
    perror ("Client recv(): ");
                  exit(
    1);
                }        
             
                  
    printf("\nThe number of digit received: %d\n"bytes_received);
                  
    recv_data[bytes_received] = '\0';

                  if (
    strcmp(recv_data"q") == || strcmp(recv_data "Q") == 0)
                  {
                    
    send((int) arg"q"strlen("q"), 0);
    //                close((int) arg);
                    
    break;
                  }

                  else 
                  
    printf("\n RECEIVED DATA = %s \n" recv_data);
                  
    fflush(stdout);
                  }
                  
    //              if (pthread_cancel (send_thr)!= 0)
    //              perror ("Thread cancelation ");              ;              

    }[/CODE
    When i compile this code with the error shown.

    Code:
    shufei@shufei-desktop:~/Desktop$ gcc -lpthread threadclient.c -o threadclient
    threadclient.c: In function ‘cthr_send’:
    threadclient.c:114: error: ‘recv_thr’ undeclared (first use in this function)
    threadclient.c:114: error: (Each undeclared identifier is reported only once
    threadclient.c:114: error: for each function it appears in.)
    is anyone know why it say the recv_thr is undeclare since it was declared in secondary thread. Please forgive my if this is an stupid question.

    Regards,
    wssoh
    Last edited by wssoh85; 12-16-2008 at 08:07 AM. Reason: Explaine about the program

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. CreateThread ?!
    By Devil Panther in forum Windows Programming
    Replies: 13
    Last Post: 11-15-2005, 10:55 AM
  4. pointer to main thread from worker thread?
    By draegon in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 06:35 AM
  5. Critical Sections, destroying
    By Hunter2 in forum Windows Programming
    Replies: 4
    Last Post: 09-02-2003, 10:36 PM