gpt4 book ai didi

c - 多线程,id不起作用,如何添加其他功能

转载 作者:行者123 更新时间:2023-11-30 15:39:35 25 4
gpt4 key购买 nike

我从每个线程中得到的 ID 都是错误的,我怎样才能获得正确的 ID?我必须再创建一个像编码(marshal)一样的线程,允许代理saing,我该如何解决这个问题?我使用讲台作为监视器来锁定或解锁互斥锁,该互斥锁只允许一个线程访问。

我有这个代码:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <linux/sched.h>

typedef enum
{ false = 0, true } t_bool;

pthread_mutex_t mutex;
int rostrum;
rostrum = 0;

void *
deputy (void *arg)
{
int tid = (int *) arg;
printf ("Deputy no: %d in f() \n", tid); // ??? no.1

int SAID;
SAID = 0;

while (SAID == 0)
{
pthread_mutex_trylock (&rostrum);
if (rostrum == 0)
{
rostrum = 1;
printf ("\t Deputy no: %d is saing\n", tid);
SAID = 1;
}
pthread_mutex_unlock (&rostrum);
}
}

int
main ()
{
pthread_t tid;
int i = 0;
for (i; i < 20; i++)
{
/* spurious characters deleted here */
printf ("Deputy no: %d before f().\n", i); // ??? no.2
pthread_create (&tid, NULL, deputy, &tid);
}
}

结果:

    Deputy no: 0 before f()
Deputy no: 1 before f()
Deputy no: 2 before f()
Deputy no: -863940960 inf f()
Deputy no: -863940960 inf f()
Deputy no: -863940960 is saing
Deputy no: -863940960 is saing
Deputy no: 3 before f()
Deputy no: -863940960 inf f()
Deputy no: 4 before f()
Deputy no: -863940960 inf f()
Deputy no: -863940960 is saing
Deputy no: 5 before f()
Deputy no: -863940960 inf f()
Deputy no: -863940960 is saing
Deputy no: 6 before f()
Deputy no: 7 before f()
Deputy no: -863940960 inf f()
Deputy no: 8 before f()
Deputy no: -863940960 inf f()
Deputy no: -863940960 is saing
Deputy no: 9 before f()
Deputy no: 10 before f()
Deputy no: 11 before f()
Deputy no: -863940960 inf f()
Deputy no: -863940960 is saing
Deputy no: -863940960 is saing
Deputy no: -863940960 is saing
Deputy no: 12 before f()
Deputy no: -863940960 inf f()
Deputy no: -863940960 inf f()
Deputy no: -863940960 is saing
Deputy no: -863940960 inf f()
Deputy no: -863940960 inf f()
Deputy no: -863940960 is saing
Deputy no: 13 before f()
Deputy no: -863940960 is saing
Deputy no: -863940960 is saing
Deputy no: 14 before f()
Deputy no: -863940960 inf f()
Deputy no: -863940960 inf f()
Deputy no: -863940960 is saing
Deputy no: -863940960 is saing
Deputy no: 15 before f()
Deputy no: -863940960 inf f()
Deputy no: 16 before f()
Deputy no: -863940960 inf f()
Deputy no: -863940960 is saing
Deputy no: -863940960 is saing
Deputy no: -863940960 inf f()
Deputy no: 17 before f()
Deputy no: 18 before f()
Deputy no: -863940960 inf f()
Deputy no: -863940960 inf f()
Deputy no: -863940960 is saing
Deputy no: 19 before f()
Deputy no: -863940960 is saing
Deputy no: -863940960 is saing

最佳答案

在你的main()中你有

pthread_t tid;

您没有初始化它,因此它的内容是未定义的。

您将指向此的指针作为您在 pthread_create 上的参数传递:

pthread_create (&tid, NULL, deputy, &tid);

我认为您希望将 &tid 指定为 pthread_create 的第一个参数将覆盖 tid 中的值。然而,这有两个问题。首先,您无法保证它何时会发生 - printf 可能已经在新线程中运行。第二个是对 pthread_create 的后续调用可能会覆盖该值,即您可以在执行第一个中的 printf 之前运行两个 pthread_create 调用.

然后使用它作为您的线程的“id”。

int tid = (int *) arg;
printf ("Deputy no: %d in f() \n", tid); // ??? no.1

这本质上会在每个线程中打印相同的未定义值。

如果您想获取唯一的线程 ID,请使用 pthread_self,而不是查看传递的参数并尝试将 pthread_create 调用的结果作为参数传递.

关于c - 多线程,id不起作用,如何添加其他功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21363617/

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