使用mutex_destroy()时如何释放内存:Posix多线程

时间:2014-05-26 06:14:47

标签: c multithreading posix

使用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的东西来回收空间。如果我没有错,就不需要这种情况。

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

3 个答案:

答案 0 :(得分:4)

好吧它在你在动态分配的互斥锁下提供的手册页中说明了它:

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并不会使您无法在动态分配的free结构上调用mutex_t

编辑,是的,如果你正在使用pthread库,你应该看看这里:

  

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

答案 1 :(得分:1)

在您的示例中,&#39;被破坏的互斥锁变量使用的空间&#39;是全局变量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;
}

答案 2 :(得分:0)

  

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

通过pthread_mutex_init()再次致电mutex