Thread: 3 threads problem

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

    3 threads problem

    hi,

    I have 3 threads in my program but without mutex. Now the problem is that the 3th thread stops looping...

    Do I have to use mutex and how does it work for 3 threads

    Code:
    static char buffer[64][255]
    static char buffer2[30][3000]
    
    /* in the main */
    
    iret1 = pthread_create(&thread1,NULL,(void*)&read_port,NULL);
    			iret2 = pthread_create(&thread2,NULL,(void*)&read_buffer,NULL);
    			iret3 = pthread_create(&thread3,NULL,(void*)&split_buffer,NULL);
    
    			pthread_join(thread1,NULL);
    			pthread_join(thread2,NULL);
    			pthread_join(thread3,NULL);
    
    /* thread 1 */
    
    for(EVER){
    		res=read(fd,buf,255);
    		buf[res]=0;
    		
    		if(res > 0){
    
    			strcat(buffer[i],buf);
    
    			i++;
    
    			if(i==64){
    				i=0;
    			}
    
    		}
    	}
    
    /* thread 2 */
    
    for(EVER){
    	if(a!=i){
                             /* here i get data out of first buffer and put it into second buffer */
                    }
    }
    
    /* thread 3 */
    
    for(EVER){
    
    		if(j!=c){
                                       /* here i read from the second buffer, split it in pieces and write to database */
                                     }
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    Originally posted by Salem
    Since thread 1 has a buffer overflow, I'd fix that first
    there is no overflow
    http://www.cs.cf.ac.uk/Dave/C/CE.html
    Lots of info on threads / shared memory
    thx for the url

    greetz

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    Originally posted by vVv
    First, in main( ) you can call pthread_exit( ) instead of pthread_join( )'ing all threads.

    I don't know the purpose of thread 2, but it looks like you're having some redundant overhead by copying the data twice - does thread 2 change / add to the data? Mutexes will work just fine - check out pthread_mutex_lock( ) / pthread_mutex_unlock( ). And you can use condition variables to put threads asleep until data is available (needed perhaps if ``fd'' is a pipe or socket and might not immediately return data) - check out pthread_cond_wait( ) and pthread_cond_signal( ).
    (Search the board, I posted several snippets using these functions in the past)
    the purpose of thread 2 is to get al data out of the first buffer and put them into buffer2, in buffer2 the data becoms al whole message and then ion thread 3 I can search for a date , an hour in the messages.

    the program works but at a given moment the last thread just stops writing to database, the first and the second are still running

    thx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. Closing threads
    By VirtualAce in forum Windows Programming
    Replies: 8
    Last Post: 01-02-2006, 10:49 AM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM