gpt4 book ai didi

c - 在 C 中释放结构后立即停止 pthread

转载 作者:太空宇宙 更新时间:2023-11-03 23:30:38 25 4
gpt4 key购买 nike

我有一个工作线程处理一个工作项队列。我刚刚实现了第二个工作人员来处理插入到 worker1 中的项目。但是,我在使用 Valgrind 时遇到了一些Invalid reads

我假设这是因为我传递给 worker2()struct foo 在主线程中的某个时刻被释放了。本质上 struct foo 是一个不断更新的结构(malloc/free),但是,我希望 worker2 将一些缺失的项目插入到 foo.

我的问题是:worker2是否有可能在struct fooNULL时立即停止处理?并在调用 create_foo() 时重新开始?我不确定用线程将丢失的项目插入 foo 的最佳方法是什么?感谢任何反馈。

//canonical form
//producer
void push_into_queue(char *item)
{
pthread_mutex_lock(&queueMutex);
if (workQueue.full) { // full }
else
{
add_item_into_queue(item);
pthread_cond_signal(&queueSignalPush);
}
pthread_mutex_unlock(&queueMutex);
}
}

// consumer1
void *worker1(void *arg)
{
while (true) {
pthread_mutex_lock(&queueMutex);
while (workQueue.empty)
pthread_cond_wait(&queueSignalPush, &queueMutex);

item = workQueue.front; // pop from queue
add_item_into_list(item);

pthread_cond_broadcast(&queueSignalPop);
pthread_mutex_unlock(&queueMutex);
}
return NULL;
}

pthread_create(&thread1, NULL, (void *) &worker, NULL);

// consumer2
void *worker2(void *arg)
{
my_struct *foo = (my_struct *) arg;
while (true) {
pthread_mutex_lock(&queueMutex);
while (list.empty)
pthread_cond_wait(&queueSignalPop, &queueMutex);

for (i = 0; i < list.size; i++)
insert_item_into_foo(list[i].item, foo);
pthread_cond_broadcast(&queueSignalPop);
pthread_mutex_unlock(&queueMutex);
}
return NULL;
}

void create_foo()
{
my_struct *foo = calloc(10, sizeof(my_struct));
pthread_create(&thread2, NULL, (void *) &worker2, foo);
}

void free_foo()
{
pthread_mutex_lock(&queueMutex);
int i;
for (i=0; i<5; i++)
free(foo[i].list->string);
free(foo[i].list);
free(foo);
pthread_mutex_unlock(&queueMutex);
}

最佳答案

您没有为 worker1worker2 定义任何终止条件。我想 foo 的 eol 可以这样考虑。这意味着两个工作人员都必须通过拥有对 foo 的引用(即 foo **)来监视 foo 的存在。

void *worker2(void *arg)
{
my_struct **foo = (my_struct **) arg;
while(true) {
pthread_mutex_lock(&queueMutex);
while (list.empty)
pthread_cond_wait(&queueSignalPop, &queueMutex);
if (NULL == *foo)
break;
for (i = 0; i < list.size; i++)
insert_item_into_foo(list[i].item, *foo);
pthread_cond_broadcast(&queueSignalPop);
pthread_mutex_unlock(&queueMutex);
}
free(foo);
return NULL;
}

void create_foo()
{
my_struct *foo = calloc(10, sizeof(my_struct ));
my_struct **foo_ptr = malloc(1, sizeof(my_struct *));
*foo_ptr = foo;
pthread_create(&thread2, NULL, (void *) &worker2, foo_ptr);
// more work with foo
}

请注意,必须以某种方式将 foo 分配给不同的变量,以便在 free_foo 中可以访问(您的代码假设了这一事实而没有明确显示它 - 因此我的评论在create_foo 的结尾)。

在上面的代码中,worker2 的每个实例都拥有一个在其整个生命周期中都依赖的指针,并且在退出之前必须处理好这个指针。

更新:

也许更好的解决方案是将结构传递给 thread2,其中包含 foo 指针,以及指示该指针是否仍然有效的标志。您可以在 struct ad lib 中添加线程所需的任何其他信息。

struct th2_data {
enum {RUNNING, TERMINATING} state;
my_struct *foo;
};

然后分配该结构的实例,将其初始化为{RUNNING, foo},并将其传递给thread2。在某处保留其地址的副本,以便能够向 thread2 发出 TERMINATING 状态信号。实际上,正如您在评论中所问,您必须将线程 2 中的 if (NULL == *foo) 测试替换为 if (foo.state == TERMINATING) .

关于c - 在 C 中释放结构后立即停止 pthread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16555261/

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