Thread: get error when I use thread

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    5

    Question get error when I use thread

    I want to use thread in my Client program to run functions together but I get this error pure virtual method called terminate called without an active exception and the program stops,what should I do?

    Code:
    bool setup::conn() {
    
    	struct sockaddr_in serv_addr;
    	struct hostent *server;
    
    	sock = socket(AF_INET, SOCK_STREAM, 0);
    	if (sock == -1){
    		printf("ERROR opening socket");
    		fflush(stdout);
    	}
    
    	server = gethostbyname("192.168.1.113");
    	if (server <= 0) {
    		fprintf(stderr, "ERROR, no such host\n");
    		fflush(stdout);
    	}
    	bzero((char *) &serv_addr, sizeof(serv_addr));
    	serv_addr.sin_family = AF_INET;
    	bcopy((char *) server->h_addr,
    	(char *)&serv_addr.sin_addr.s_addr,
    	server->h_length);
    	serv_addr.sin_port = htons(MYPORT);
    
    	while (true) {
    		fflush(stdout);
    		csock = (int*) malloc(sizeof(int));
    		*csock = connect(sock, (struct sockaddr *) &serv_addr,
    				sizeof(serv_addr));
    		if (*csock == -1) {
    			perror("error");
    			if (errno == EISCONN  )
    				break;
    			
    
    		}
    
    		thread thread1;
    		thread1 = thread(&setup::SocketHandler, this);
    		 thread1.join();
         }
    }
    void setup::SocketHandler() {
    
    	while (1) {
    
    		if ((bytecount = recv(*csock, buffer, buffer_len, 0)) == -1)
    			fprintf(stderr, "Error receiving data %d\n", errno);
    
    		if (bytecount > 0) {
    			printf("\n Received bytes: %d ", bytecount);
    			
    		}
                    fflush(stdout);
    		usleep(100);
    
    	}
    	pthread_exit(0);
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. Why so much C code?
    2. You malloc for no good reason, and never free it.
    3. Study this thread::thread - C++ Reference
    4. I'm pretty sure calling pthread_exit breaks everything. Find out how you're supposed to exit a std:thread.
    5. With the join where it is, you're not using threads with any effectiveness.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I suppose the first step is to create a mockup test you can work with for the purpose of understanding threads.

    Then you can add socket to it.

    But FWIW, using select() with timeouts allows you to do pretty much whatever you want without the massive complications that threads bring.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error monitoring thread status
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 07-15-2008, 07:48 AM
  2. Basic GUI and odd thread error
    By woofer in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2006, 09:26 AM
  3. Assertion Error with DoModal(), activated from a thread
    By hanhao in forum Windows Programming
    Replies: 10
    Last Post: 07-12-2005, 08:57 PM
  4. Assertion Error with DoModal(), activated from a thread
    By hanhao in forum Windows Programming
    Replies: 1
    Last Post: 07-08-2005, 12:39 AM
  5. Thread error (errno 11)
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 04-03-2003, 06:12 PM

Tags for this Thread