C - 使用 PThreads 时更快地锁定整数

发布于 2024-12-10 10:42:50 字数 589 浏览 0 评论 0原文

我有一个计数器,多个线程使用它来写入数组中的特定元素。这是我到目前为止所拥有的......

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
}

问题是它太慢了。我接受一个整数文件作为 I/O 重定向并将它们写入数组。看起来每个线程都花了很多时间等待锁被移除。有没有更快的方法来增加计数器?

注意:我需要保持数字的顺序,这就是为什么我必须使用计数器而不是为每个线程提供要写入的特定数组块。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余生一个溪 2024-12-17 10:42:50

您需要使用互锁。查看互锁* 函数,或苹果的 OSAtomic* 函数,或者可能是 libatomic在Linux上。

如果您有一个很好支持 C++11 的编译器,您甚至可以使用 std::atomic

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.

那请放手 2024-12-17 10:42:50

好吧,一种选择是在将批处理应用于受保护的资源之前在本地某处批量处理更改。

例如,让每个线程收集条信息(如果在收集十条之前就用完了,则收集更少),然后修改Foo以获取长度和数组 - 这样,你可以分摊锁定的成本,从而提高效率。

我也会非常小心地做以下事情:

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

在保护区之外 - 这会导致灾难,因为另一个线程可能会从您下面更改 currentCount。如果它是一个本地变量,那不是问题,因为每个线程都有自己的副本,但从代码中不清楚该变量的作用域。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文