“新”多线程中的运算符会导致分段错误

发布于 2024-09-16 17:55:50 字数 1531 浏览 3 评论 0 原文

这与我此处此处,但由于我的调查使我离开从STL作为潜在问题,到“新”作为我的敌人,我认为最好开始一个新线程。

重申一下,我使用的是嵌入式平台供应商提供的arm-linux交叉编译器(版本2.95.2)。

当我在 Linux PC 上运行下面的应用程序时,它当然永远不会失败。然而,当在嵌入式设备上运行它时,我每次都会遇到分段错误。使用“malloc”永远不会失败。使用互斥体同步“新”分配将阻止该问题,但这在我的主要应用程序中并不实用。

任何人都可以建议为什么会发生这种情况,或者有任何想法如何解决这个问题?

谢谢。

#include <stdio.h>
#include <pthread.h>


pthread_mutex_t _logLock = PTHREAD_MUTEX_INITIALIZER;

static void* Thread(void *arg)
{
    int i = 0;
    while (i++ < 500)
    {
        // pthread_mutex_lock(&_logLock);
        char* myDyn = (char*) new char[1023];

        //        char* buffer = (char*) malloc(1023);
        //        if (buffer == NULL)
        //            printf("Out of mem\n");
        //        free(buffer);


        delete[] myDyn;

        //pthread_mutex_unlock(&_logLock);


    }
    pthread_exit(NULL);
}

int main(int argc, char** argv)
{
    int threads = 50;
    pthread_t _rx_thread[threads];
    for (int i = 0; i < threads; i++)
    {
        printf("Start Thread: %i\n", i);
        pthread_create(&_rx_thread[i], NULL, Thread, NULL);
    }

    for (int i = 0; i < threads; i++)
    {
        pthread_join(_rx_thread[i], NULL);
        printf("End Thread: %i\n", i);
    }
}

This is related to an issue I have been discussing here and here, but as my investigations have led me away from the STL as the potential issue, and towards "new" as my nemisis, I thought it best to start a new thread.

To reiterate, I am using an arm-linux cross compiler (version 2.95.2) supplied by the embedded platform vendor.

When I run the application below on my Linux PC, it of course works never fails. However when running it on the embedded device, I get segmentation faults every time. Using "malloc" never fails. Synchronising the "new" allocation using the mutex will stop the issue, but this is not practical in my main application.

Can anyone suggest why this might be occurring, or have any ideas how I can get around this issue?

Thanks.

#include <stdio.h>
#include <pthread.h>


pthread_mutex_t _logLock = PTHREAD_MUTEX_INITIALIZER;

static void* Thread(void *arg)
{
    int i = 0;
    while (i++ < 500)
    {
        // pthread_mutex_lock(&_logLock);
        char* myDyn = (char*) new char[1023];

        //        char* buffer = (char*) malloc(1023);
        //        if (buffer == NULL)
        //            printf("Out of mem\n");
        //        free(buffer);


        delete[] myDyn;

        //pthread_mutex_unlock(&_logLock);


    }
    pthread_exit(NULL);
}

int main(int argc, char** argv)
{
    int threads = 50;
    pthread_t _rx_thread[threads];
    for (int i = 0; i < threads; i++)
    {
        printf("Start Thread: %i\n", i);
        pthread_create(&_rx_thread[i], NULL, Thread, NULL);
    }

    for (int i = 0; i < threads; i++)
    {
        pthread_join(_rx_thread[i], NULL);
        printf("End Thread: %i\n", i);
    }
}

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

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

发布评论

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

评论(4

执手闯天涯 2024-09-23 17:55:51

如果您设备上的堆不是线程安全的,那么您需要锁定。您可以编写自己的 new 和 delete 函数,在 new 或删除期间锁定 - 您不需要在分配的内存的整个生命周期中保持锁定。

检查是否有编译器开关使分配器线程安全。

If the heap on your device isn't thread-safe, then you need to lock. You could just write your own new and delete functions that lock for the duration of new or delete -- you don't need to hold the lock across the whole lifetime of the allocated memory.

Check to see if there are compiler switches to make the allocator thread-safe.

蓝咒 2024-09-23 17:55:51

正如其他人所说,工具集的默认内存分配行为很可能不是线程安全的。我刚刚检查了我最常使用的 2 个 ARM 交叉开发工具集,其中之一确实是这种情况。

大多数工具集都提供了使库线程安全的方法,通过重新实现函数或链接到库的不同(线程安全)版本。如果没有您提供的更多信息,很难说您的特定工具集正在做什么。

虽然不想这么说,但最好的信息可能在您的工具集文档中。

As others have stated, chances are that the toolset's default memory allocation behavior is not thread-safe. I just checked with the 2 ARM cross-development toolsets I use the most, and indeed this is the case with one of them.

Most toolsets offer ways of making the libraries thread-safe, either by re-implementing functions or by linking in a different (thread-safe) version of the library. Without more information from you, it's hard to say what your particular toolset is doing.

Hate to say it, but the best information is probably in your toolset documentation.

最佳男配角 2024-09-23 17:55:51

是的,new 可能不是线程安全的。您需要围绕内存分配和删除的同步机制。查看 Boost.thread 库,它提供了互斥体类型应该可以帮助你。

Yeah, new is probably not threadsafe. You need synchronization mechanisms around the memory allocation and, separately, around the deletion. Look into the Boost.thread library, which provides mutex types that should help you out.

爱她像谁 2024-09-23 17:55:51

如何使用 malloc (你说它在嵌入式平台上永远不会失败)来获取所需的内存,然后使用放置 new (void* operator new[] (std::size_t size, void* ptr) throw()) 假设它可用,用于施工。看
新[]运算符

另外请参阅 stackoverflow 文章


MSDN

How about using malloc (you say it never fails on the embedded platform) to get the required memory then using placement new (void* operator new[] (std::size_t size, void* ptr) throw()) assuming it is available, for construction. See
new[] operator

Also see stackoverflow article

and
MSDN

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