简体   繁体   中英

How do you free() a buffer pointer in a secondary thread, if the pointer was created with malloc() in the main thread?

I am trying to implement a solution based partly on the discussions below. Basically, I want to malloc() in the main thread and free() in a secondary thread. The discussions linked deal with LabWindows, but I think my question is more aimed at C programmers in general.

LabWindows: implementing thread safe queues that can handle string elements

How to use a thread safe queue to store strings

I create a pointer to char in the main thread and allocate storage using malloc() . I copy some data into the storage, and assign the pointer to an array element ( CmtWriteTSQData expects an array). This array gets passed to a thread safe queue.

In a secondary thread, the thread safe queue is read. The data is assigned to a new array.

How do I free the memory allocated within the secondary thread, as the pointer variable is no longer in scope?

Can I just call free() on the array element? Or do I need to create another pointer to char in the secondary thread, copy the array element to it, and then call free() on the pointer?

There doesn't appear to be a return value with free() , so I can't figure out how to ensure the call succeeds.

// Main thread
char *ptr = NULL;
char *array1[1] = {0};

ptr = (char *) malloc (3 * sizeof (char));

strcpy (ptr, "hi");

array1[0] = ptr;

CmtWriteTSQData (queue, array1, 1, 0 NULL);    

// Secondary thread
char *array2[1] = {0};

CmtReadTSQData (queue, array2, 1, 0, 0);

printf ("%s", array2[0]);  // Prints "hi"

free (array2[0]);  // Does this work?

The short answer is yes.

When you call malloc() , you are asking the operating system to give you a chunk of usable memory of some minimum size, and the operating system responds by passing you a pointer, which is just an integer representing the virtual address of that chunk of memory.

When a program has multiple threads, this means that several lightweight processes are sharing the same virtual address space, which means if there are several copies of some valid pointer among several threads of the same process, then they must all point to the same memory location.

The operating system does not care which thread asked for the memory, and it does not care which thread gives it back. When malloc() returns a pointer in a process, all of the threads in that process may use it, and when one of the threads free() s that pointer, that memory location becomes invalid for all threads in the process.

I don't know how the functions CmtWriteTSQData() and CmtReadTSQData() behave, but as long as printf("%p\n",ptr) in main and printf("%p\n",array2[0]) in the secondary thread produce the same hex value, your code is good.

Hope that helps!

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