【问题标题】:When using mutex_destroy() How do I free memory: Posix multithreading使用 mutex_destroy() 时如何释放内存:Posix 多线程
【发布时间】:2014-05-26 06:14:47
【问题描述】:

使用mutex synchronization的示例代码:

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

pthread_mutex_t mutex;
int sharedData=100;

void* criticalSection(void* arg)
{
  pthread_mutex_lock(&mutex);
  sharedData+=100;
  printf("Shared Data has been modified to %d by thread %d\n",sharedData,pthread_self());
  pthread_mutex_unlock(&mutex);
  pthread_exit(NULL);
}




int main()
{
    int rc;
        pthread_t p1,p2;
    rc = pthread_mutex_init(&mutex,NULL);
    if (rc!=0)printf("Mutex init failed at %d %s ",__LINE__,__func__ );

    pthread_create(&p1,NULL,criticalSection,NULL);
    pthread_create(&p2,NULL,criticalSection,NULL);
    pthread_join(p1,NULL);
    pthread_join(p2,NULL);
    rc = pthread_mutex_destroy(&mutex);
    if (rc!=0)printf("Mutex destroy failed at %d %s ",__LINE__,__func__ );

    return 0;
}

文档说:

http://docs.oracle.com/cd/E19455-01/806-0630/6j9vkb8e0/index.html

Destroy
mutex_destroy() destroys the mutex object referenced by mp ; the mutex object 
becomes uninitialized. 

The space used by the destroyed mutex variable is not freed.
It needs to be explicitly reclaimed.

那么我们如何回收空间呢?在上面的例子中,是否需要回收空间?如果是怎么办?

我想如果mutex_t 是在堆上创建的,我们会使用delete 之类的东西来回收空间。在这种情况下不需要,如果我没记错的话。

有人可以举一个需要空间回收的例子吗?或者说如何在heap上初始化mutex_t对象?

【问题讨论】:

  • 这是posix的多线程而不是oracle的。

标签: c multithreading posix


【解决方案1】:

嗯,它就在您在 Dynamically Allocated Mutexes 下提供的手册页中说明了这一点:

struct record {
            int field1;
            int field2;
            mutex_t m;
} *r;
r = malloc(sizeof(struct record));
mutex_init(&r->m, USYNC_THREAD, NULL);
/*
  * The fields in this record are accessed concurrently
  * by acquiring the embedded lock.
  */

到最后:

for (i = 0; i < 2; i++)
       thr_join(0, 0, 0);
mutex_destroy(&r->m);     /* first destroy mutex */
free(r);                 /* Then free memory */

这就是你要找的吗?

您引用的评论仅意味着调用mutex_destroy 并不能免除您在动态分配的mutex_t 结构上调用free。

编辑,是的,如果您使用的是 pthread 库,您可能应该看这里:

http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_destroy.html

【讨论】:

  • 谢谢.. 搞糊涂了——pthread_destroy() 到底是做什么的
【解决方案2】:

在您的示例中,“被破坏的互斥变量使用的空间”是全局变量mutex。所以,它的空间会在进程退出时自动销毁。

pthread_mutex_t 是一个结构类型。只是一小块内存。您可以将其分配在静态数据部分(如您的示例),或通过普通的malloc 调用在堆上分配。

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

pthread_mutex_t* pmutex;
int sharedData=100;

void* criticalSection(void* arg)
{
  pthread_mutex_lock(pmutex);
  sharedData+=100;
  printf("Shared Data has been modified to %d by thread %d\n",sharedData,pthread_self());
  pthread_mutex_unlock(pmutex);
  pthread_exit(NULL);
}


int main()
{
    int rc;
    pthread_t p1,p2;

    pmutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));

    rc = pthread_mutex_init(pmutex,NULL);
    if (rc!=0)printf("Mutex init failed at %d %s ",__LINE__,__func__ );

    pthread_create(&p1,NULL,criticalSection,NULL);
    pthread_create(&p2,NULL,criticalSection,NULL);
    pthread_join(p1,NULL);
    pthread_join(p2,NULL);
    rc = pthread_mutex_destroy(pmutex);
    if (rc!=0)printf("Mutex destroy failed at %d %s ",__LINE__,__func__ );

    free(pmutex);

    return 0;
}

【讨论】:

  • 你确定不会干扰 pthread_mutex_init 调用吗?
  • 如果 init 不希望引用已经初始化。
  • @9dan 我想pmutext = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)); rc = pthread_mutex_init(pmutex,NULL); 是问题
  • struct record { int field1; int field2; mutex_t m; } *r; r = malloc(sizeof(struct record)); mutex_init(&amp;r-&gt;m, USYNC_THREAD, NULL); 或 pmutext = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)); rc = pthread_mutex_init(pmutex,NULL); 不是一样的初始化吗?
  • @Vlad(抱歉之前的输入错误)它正在传递未初始化的互斥对象。您是否偶然将未初始化的内存与未初始化的互斥对象混淆了?
【解决方案3】:

那么我们如何回收空间呢?

通过在mutex 上调用pthread_mutex_init()(再次)。

【讨论】:

  • 你的意思是说..空间还没有释放..所以我们可以重用它,通过在同一个mutex_t上调用pthread_mutex_init()创建一个新的mutex object?跨度>
  • 分配和释放一个持有互斥锁的变量是一回事,另一个是初始化和销毁​​互斥锁​​。 @GauravK
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-14
  • 2018-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-13
  • 1970-01-01
相关资源
最近更新 更多