My thread stops for no reason

This is a discussion on My thread stops for no reason within the C Programming forums, part of the General Programming Boards category; hi, I have 2 threads in my program, and for some reason the second thread stops for no reason. there ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    20

    Unhappy My thread stops for no reason

    hi,

    I have 2 threads in my program, and for some reason the second thread stops for no reason.

    there is a
    Code:
    for(;;){
    
    }
    loop in both of teh threads, does anyone had this problem too?

    what do i have to do, so the thread does not get sheduled out?

    thx

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Well; you have to make sure that none of the for loops are infinite.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    20

    Unhappy

    but they must be infinite, the program is not supposed to end, neither does the thread

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Then you have to put all processing in the for loops, or the program will lock up. Maybe you should post some code?

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    the first thread reads from com port en puts in buffer, the second thread reads from buffer and write (via function) to file.
    The second thread stops without a reason , sometimes at that moment and then on an other moment, i really don't know what the problem could be

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    How do you synchronize access to the buffer?
    Do you close the file after write, or are you waiting for access to the file?

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    here is the code, and i've tried mutex but is does strange things, in the code you see my mutex to, it now only as comment

    Code:
    #include <stdio.h>
    #include <termios.h>
    #include <fcntl.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <sys/io.h>
    #include <dirent.h>
    #include <string.h>
    #include <stdlib.h>
    #include <mysql/mysql.h>
    #include <pthread.h>
    #include <unistd.h>
    
    #define _REENTRANT
    #define PORT_DEV "/dev/ttyS0" /* This is COM1 port, try ttyS1 for COM2*/
    #define BAUDRATE B9600
    #define _POSIX_SOURCE 1
    #define FALSE 0
    #define TRUE 1
    #define CR 0x0d
    #define LF 0x0a
    #define FF 0x0c
    #define SX 0x02
    #define EX 0x03
    #define EVER ;;
    #define BUFNUM 128
    
    
    
    static char buffer[BUFNUM][256];
    static char buf[60][3000];
    int i=0,c=0;
    int res,fd;
    struct bufadmi{
    	pthread_mutex_t	buflock;
    	pthread_cond_t bufcond;
    	int		bufvol;
    };
    
    struct bufadmi ba[BUFNUM];
    
    void *read_port();
    void *read_buffer(pthread_t *);
    void write_buffer(char *buffer2);
    
    main()
    {	
    
    	DIR *dirp;
    	struct termios oldtio, newtio;
    	pthread_t thread1,thread2;
    	pthread_attr_t thrattr;
    	int iret1,iret2;
    
    	int j=0;
    	
    	//CHECK ID DIRECTORY EXIST
    
    	if((dirp=opendir("log"))==NULL){
    		printf("Directory <log> does not exist, please create it\n");
    		exit(0);
    	}
    	if((dirp=opendir("log/AFTN"))==NULL){
    		printf("Directory <log/AFTN> does not exist, please create it\n");
    		exit(0);
    	}
    	if((dirp=opendir("log/ATS"))==NULL){
    		printf("Directory <log/ATS> does not exist, please create it\n");
    		exit(0);
    	}
    
    	//OPEN COM PORT
    
    	fd = open(PORT_DEV, O_RDONLY | O_NOCTTY | O_NONBLOCK | O_APPEND);
    	if (fd < 0)
    	{
    		perror("Opening port");
    		exit(-1);
    	}
    	else if(fd > 0)
    	{
    		perror("Opening port");
    
    
    			//INITIALIZATION SERIAL PORT
    			tcgetattr(fd,&oldtio);
    			bzero(&newtio, sizeof(newtio));
    
    			newtio.c_cflag = BAUDRATE |CS8 | CLOCAL | CREAD;
    			newtio.c_iflag = IGNPAR;
    			newtio.c_oflag = 0;
    			newtio.c_lflag = 0;
    
    			newtio.c_cc[VMIN]=255;
    			newtio.c_cc[VTIME]=0;
    
    			//tcflush(fd, NULL);
    			tcsetattr(fd,TCSANOW,&newtio);
    
    			fcntl(fd, F_SETFL,0);
    
    			//START READING FROM PORT
    			pthread_attr_init(&thrattr);
    			pthread_attr_setscope(&thrattr, PTHREAD_SCOPE_SYSTEM);
    
    			if((iret1 = pthread_create(&thread1,&thrattr,read_port,NULL))){
    				fprintf(stderr,"pthread1: %s\n",strerror(iret1));
    				exit(1);
    			}else printf("pthread1 succesful\n");
    			if((iret2 = pthread_create(&thread2,&thrattr,(void*)(pthread_t *)read_buffer,&read_port))){
    				fprintf(stderr,"pthread2: %s\n",strerror(iret2));
    				exit(1);
    			}else printf("pthread2 succesful\n");
    
    
    			for(j=0;j<BUFNUM;j++){
    				pthread_mutex_init(&ba[j].buflock,NULL);
    				pthread_cond_init(&ba[j].bufcond,NULL);
    			}
    			//pthread_join(thread1,NULL);
    			//pthread_join(thread2,NULL);
    			pthread_exit(NULL);
    			
    
    	}
    	tcsetattr(fd,TCSANOW,&oldtio);
    }
    
    void * read_port(void)
    {
    	char buf[256];
    	for(EVER){
    		/*pthread_mutex_lock(&ba[i].buflock);
    
    		while(ba[i].bufvol)
    			pthread_cond_wait(&ba[i].bufcond,&ba[i].buflock);*/
    
    		//printf("loop entered\n");
    		res=read(fd,buf,255);
    		buf[res]='\0';
    
    		//printf("%i\n",res);
    		//printf("Dit waren 255 chars\n");
    
    		if(res > 0){
    			strcat(buffer[i],buf);
    			//ba[i].bufvol = 1;
    			printf("---coming form port---\n%s\n---end---\ni = %i\n",buffer[i],i);
    			i++;
    			//printf("Dit waren %d chars, i is nu %d\n",strlen(buf),i);
    			if(i==BUFNUM){
    				i=0;
    
    
    			}
    			/*pthread_cond_signal(&ba[i].bufcond);
    			pthread_mutex_unlock(&ba[i].buflock);*/
    
    		}
    	}
    }
    
    
    void * read_buffer(pthread_t *read_port)
    {	
    	int a=0,b=0,d=0,z=0;
    
    	for(EVER){
    		//printf("a = %i\n",a);
    		/*pthread_mutex_lock(&ba[a].buflock);
    
    			while(ba[a].bufvol)
    				pthread_cond_wait(&ba[a].bufcond,&ba[a].buflock);*/
    
    		if(i==a){}else{
    				//printf("buffer[a][b] comes in buf[c][d]\n");
    				buf[c][d]=buffer[a][b];
    				//printf("%i %i %i %i\n",a,b,c,d);
    				d++;
    
    				if(buffer[a][b]==FF){
    					printf("FF found\n");
    					buf[c][d++]='\0';
    
    					printf("buffer lengte is %d\n",strlen(buf[c]));
    					printf("---begin message---\n%s\n---end of message---\n",buf[c]);
    
    					write_buffer(buf[c]);
    
    					c++;
    					printf("\nc gets %i",c);
    					d=0;
    
    				}
    				b++;
    
    				if(b==strlen(buffer[a])){
    					printf("i = %d and a = %d\n",i,a);
    										b=0;
    					/*ba[a].bufvol=0;
    					pthread_cond_signal(&ba[a].bufcond);
    					pthread_mutex_unlock(&ba[a].buflock);*/
    
    					for(z=0;z<256;z++){
    						buffer[a][z]='\0';
    					}
    					a++;
    				}
    				if(a==BUFNUM){
    										a=0;
    				}
    				if(d==3000){
    										d=0;
    				}
    
    				if(c==60){
    										c=0;
    				}
    
    			}
    
    
    
    	}
    }

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    thx i'll look at it, i'll compare the mutexes, maybe thats the problem after all

    greetz

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terminating secondary thread from another thread
    By wssoh85 in forum C++ Programming
    Replies: 13
    Last Post: 12-19-2008, 04:14 AM
  2. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  3. C++ Threading?
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 12:16 PM
  4. [code] Win32 Thread Object
    By Codeplug in forum Windows Programming
    Replies: 0
    Last Post: 06-03-2005, 03:55 PM
  5. multithreading question
    By ichijoji in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2005, 10:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21