简体   繁体   中英

C - faster locking of integer when using PThreads

I have a counter that's used by multiple threads to write to a specific element in an array. Here's what I have so far...

int count = 0;
pthread_mutex_t count_mutex;

void *Foo()
{
    // something = random value from I/O redirection
    pthread_mutex_lock(&count_mutex);
    count = count + 1;
    currentCount = count;
    pthread_mutex_unlock(&count_mutex);
    // do quick assignment operation. array[currentCount] = something
}
main()
{
    // create n pthreads with the task Foo
}

The problem is that it is ungodly slow. I'm accepting a file of integers as I/O redirection and writing them into an array. It seems like each thread spends a lot of time waiting for the lock to be removed. Is there a faster way to increment the counter?

Note: I need to keep the numbers in order which is why I have to use a counter vs giving each thread a specific chunk of the array to write to.

You need to use interlocking. Check out the Interlocked* function on windows, or apple's OSAtomic* functions, or maybe libatomic on linux.

If you have a compiler that supports C++11 well you may even be able to use std::atomic .

Well, one option is to batch up the changes locally somewhere before applying the batch to your protected resource.

For example, have each thread gather ten pieces of information (or less if it runs out before it's gathered ten) then modify Foo to take a length and array - that way, you amortise the cost of the locking, making it much more efficient.

I'd also be very wary of doing:

// do quick assignment operation. array[currentCount] = something

outside the protected area - that's a recipe for disaster since another thread may change currentCount from underneath you. That's not a problem if it's a local variable since each thread will have its own copy but it's not clear from the code what scope that variable has.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM