- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我从每个线程中得到的 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/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!