Thread: pthread_mutex problem

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    351

    pthread_mutex problem

    Hi All,

    Consider this [multi-threaded] code fragment...

    Code:
    int volatile num_threads = 0;
    
    snip...
    
    if(num__threads < MAX_NUM_THREADS)
    {
    	//mutex
    	pthread_mutex_lock(&my_thread_mutex);
    	//increment number of used threads
    	num_threads++;
    	pthread_mutex_unlock(&my_thread_mutex);
    	//mutex
    
    	printf("%d:DEBUG: num threads = %d\n",pthread_self(),num_threads);
    
    	//do work
    	if(do_work() < 0)
    	{
    		//mutex
    		pthread_mutex_lock(&my_thread_mutex);
    		//decrement number of slaves
    		num_threads--;
    		pthread_mutex_unlock(&my_thread_mutex);
    		//mutex
    
    		return 1;
    	}
    
    	//mutex
    	pthread_mutex_lock(&my_thread_mutex);
    	//decrement number of slaves
    	num_threads--;
    	pthread_mutex_unlock(&my_thread_mutex);
    	//mutex
    
    	return 0;
    }
    I'm trying to throttle the number of pthreads accessing this bit of code. The mutexes are initialised and destroyed elsewhere.

    The problem is that num_threads ends up negative i..e somehow the decrement is happening more than the increment.

    Can anyone see why?

    Are my mutexes used correctly?

    Thanks for any help,

    rotis23
    Last edited by rotis23; 11-16-2005 at 09:29 AM.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    It looks fine. Is num__threads being modified anywhere else? By the way, I recommend the following small change:

    Code:
    int volatile num_threads = 0;
    
    snip...
    
    if(num__threads < MAX_NUM_THREADS)
    {
            int result;
    	//mutex
    	pthread_mutex_lock(&my_thread_mutex);
    	//increment number of used threads
    	num_threads++;
    	pthread_mutex_unlock(&my_thread_mutex);
    	//mutex
    
    	printf("%d:DEBUG: num threads = %d\n",pthread_self(),num_threads);
    
    	//do work
            result = do_work();
    
    	//mutex
    	pthread_mutex_lock(&my_thread_mutex);
    	//decrement number of slaves
    	num_threads--;
    	pthread_mutex_unlock(&my_thread_mutex);
    	//mutex
    
            if (result < 0)
                return 1;
            else
                return 0;
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Thanks for the input.

    num__threads should be num_threads in my code fragment.

    num_threads does not change anywhere else.

    Was thinking of doing that change - much more readable

    BTW - this is running on an *old* redhat 8.0 platform: any bugs on that platform that would cause this? <clutching at straws>

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Something very weird going on here...I've seen the debug printf line output MAX_NUM_THREADS+1 !

    How is that possible? Either I've implemented the mutexes wrong or they're not working properly.

  5. #5
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    RH8 [ all distros with the 2.4 kernel actually ] had a few small problems with libpthread, that were fixed in the 2.6 kernel.

    you my be hitting one of the known issues of the 2.4 kernel with pthreads.
    Quote Originally Posted by Jeff Henager
    If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.

  6. #6
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    I doubt if there were issues with the mutexes in Redhat 8. Maybe other areas, but I bet the mutexes were fairly solid. Anyway, I can see why you would end up with MAX_THREADS+1. Look at the code. You are doing your conditional check on the num_threads outside of the mutex. Do you see why that is a problem? Imagine that you are sitting at num_threads = MAX_THREADS-1 currently. Then thread1 comes along. It says, num_threads less than max, I can continue. Then context switch happens, and control goes to thread2. It looks at num_threads, says num_threads is less than max, I can continue. So then each thread obtains the mutex and increments num_threads. Now you have num_threads == MAX_THREADS+1. So change the logic to something like:

    Code:
        pthread_mutex_lock(&mutex);
    
        if (num_threads > MAX) {
            num_threads++;
    
            // do work
        }
        else {
            pthread_mutex_unlock(&mutex);
        }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Yes thanks - I can see MAX_THREADS+1 but not num_threads < 0.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Is that your real code? I don't see how that code alone can generate a num_threads value less than zero.

  9. #9
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    I think this might have been a memory violation issue. I was attempting to access a NULL struct later in the thread and I'm guessing this could have potentially violated memory?

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Anytime you attempt to dereference invalid data, strange things can (and usually do) happen in your program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM