gpt4 book ai didi

c - LP线程 : Pointer to a structure or address of a structure?

转载 作者:太空宇宙 更新时间:2023-11-04 06:51:59 25 4
gpt4 key购买 nike

我有一个创建 N 个进程的程序,每个进程创建 M 个线程。

我还有一个结构需要传递给线程函数。


当我像这样创建 M 个线程时:

thread_args_t** thread_arg = malloc(sizeof(thread_args_t*)*m);
for(int i=0;i<m;i++)
{
thread_arg[i] = malloc(sizeof(thread_args_t));
thread_arg[i]->child_id = child_id;
thread_arg[i]->thread_id = i;

pthread_create(&threads[i], NULL, thread_function, thread_arg[i]);
}

程序运行完美。


但是,如果我创建该结构的数组并传递数组的地址,程序将无法运行:

thread_args_t* thread_arg = malloc(sizeof(thread_args_t)*m);
for(int i=0;i<m;i++)
{
thread_arg[i].child_id = child_id;
thread_arg[i].thread_id = i;

pthread_create(&threads[i], NULL, thread_function, &thread_arg[i]);
}

线程处理代码如下:

void* thread_function(void* x)
{
thread_args_t *args = (thread_args_t*)x;

/* TODO: protect the critical section using semaphore(s) as needed */

//Do stuff

free(x);
pthread_exit(NULL);
}

那么,这里的问题是什么?

我在 thread_function 上遇到一个运行时错误,谈论的是无效指针。
这是输出的屏幕:

(我注意到错误类型并不总是相同的)

> [Main] Initializing file accesses.log...closed...file correctly initialized!!!
[Main] Creating named semaphore /sem_cs...Done /sem_cs: 1
[Main] Creating named semaphore /sem_ready...Done /sem_ready: 0
[Main] Creating named semaphore /sem_start...Done /sem_start: 0
[Main] Creating named semaphore /sem_closed...Done /sem_closed: 0
mainProcess All are ready. Firing. BAAAAAANG!
mainProcess RONFRONF
Child N.4 bursting 10 threads
Child N.0 bursting 10 threads
Child N.1 bursting 10 threads
Child N.2 bursting 10 threads
[Child#1-Thread#2] File accesses.log opened in append mode!!!
[Child#1-Thread#2] 1 appended to file accesses.log opened in append mode!!!
[Child#1-Thread#2] File accesses.log closed!!!
Child N.3 bursting 10 threads
[Child#0-Thread#0] File accesses.log opened in append mode!!!
*** Error in `./riepilogo_errato': double free or corruption (out): 0x0000000001c18640 ***
[Child#0-Thread#0] 0 appended to file accesses.log opened in append mode!!!
[Child#0-Thread#0] File accesses.log closed!!!
======= Backtrace: =========
Child N.0 joining threads[0]
/lib/x86_64-linux-gnu/libc.so.6([Child#1-Thread#1] File accesses.log opened in append mode!!!
+0x777e5)[0x7f0ebaacf7e5]
[Child#1-Thread#1] 1 appended to file accesses.log opened in append mode!!!
[Child#1-Thread#1] File accesses.log closed!!!
/lib/x86_64-linux-gnu/libpthread.so.0[Child#4-Thread#0] File accesses.log opened in append mode!!!
*** Error in `./riepilogo_errato[Child#4-Thread#0] 4 appended to file accesses.log opened in append mode!!!
[Child#0-Thread#4] File accesses.log opened in append mode!!!
/lib/x86_64-linux-gnu/libc.so.6[Child#0-Thread#4] 0 appended to file accesses.log opened in append mode!!!
[Child#0-Thread#4] File accesses.log closed!!!
*** Error in `./riepilogo_errato': double free or corruption (out): 0x0000000001c18650 ***
[Child#2-Thread#0] File accesses.log opened in append mode!!!
[Child#2-Thread#0] 2 appended to file accesses.log opened in append mode!!!
======= Backtrace: =========
[Child#2-Thread#0] File accesses.log closed!!!
[Child#0-Thread#1] File accesses.log opened in append mode!!!
[Child#0-Thread#1] 0 appended to file accesses.log opened in append mode!!!
[Child#0-Thread#1] File accesses.log closed!!!
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f0ebaacf7e5]
[Child#4-Thread#0] File accesses.log closed!!!
[Child#2-Thread#1] File accesses.log opened in append mode!!!
[Child#2-Thread#1] 2 appended to file accesses.log opened in append mode!!!
[Child#2-Thread#1] File accesses.log closed!!!
Child N.2 joining threads[0]
[Child#4-Thread#9] File accesses.log opened in append mode!!!
[Child#4-Thread#9] 4 appended to file accesses.log opened in append mode!!!
[Child#4-Thread#9] File accesses.log closed!!!
[Child#3-Thread#0] File accesses.log opened in append mode!!!
[Child#3-Thread#0] 3 appended to file accesses.log opened in append mode!!!
[Child#3-Thread#0] File accesses.log closed!!!
Child N.3 joining threads[0]
[Child#3-Thread#1] File accesses.log opened in append mode!!!
[Child#3-Thread#1] 3 appended to file accesses.log opened in append mode!!!
[Child#3-Thread#1] File accesses.log closed!!!
*** Error in `./riepilogo_errato': free(): invalid pointer[Child#3-Thread#2] File accesses.log opened in append mode!!!
[Child#3-Thread#2] 3 appended to file accesses.log opened in append mode!!!
[Child#3-Thread#2] File accesses.log closed!!!
*** Error in `./riepilogo_errato[Child#3-Thread#7] File accesses.log opened in append mode!!!

最佳答案

您的线程代码释放传递的指针:

void* thread_function(void* x)
{
thread_args_t *args = (thread_args_t*)x;

free(x); // <-- HERE
}

但是,这不是从分配函数返回的指针 - 它是数组中的指针。

这是未定义的行为。

修复只是删除对 free() 的调用。

关于c - LP线程 : Pointer to a structure or address of a structure?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49935470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com