我从每个帖子中得到了错误的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
答案 0 :(得分:1)
在你的main()
中
pthread_t tid;
你没有初始化,所以它的内容是未定义的。
你在pthread_create
这里传递一个指针作为你的参数:
pthread_create (&tid, NULL, deputy, &tid);
我认为您希望将&tid
指定为pthread_create
的第一个参数将覆盖tid
中的值。但是,这有两个问题。首先,您无法保证何时会发生 - printf
已经在新线程中运行了。第二个是,对pthread_create
的后续调用可能会覆盖此值,即您可以在执行第一个pthread_create
之前运行两个printf
次调用。
然后使用它作为你的主题'id'。
int tid = (int *) arg;
printf ("Deputy no: %d in f() \n", tid); // ??? no.1
这实际上会在每个线程中打印相同的未定义值。
如果要获取唯一的线程ID,请使用pthread_self
,而不是查看传递的参数,并尝试将pthread_create
调用的结果作为参数传递。
答案 1 :(得分:0)
要从线程内部获取线程的PThread-id,请使用pthread_self()
。
为此,请修改threas函数,如下所示:
void *
deputy (void * arg)
{
pthread_t ptid = pthread_self();
...
如果你真的想存储在主线程中创建的每个线程的PThread-id 和传递它的引用,你需要为每个线程提供变量,如下所示:
int
main (void)
{
pthread_t ptid[20];
{
size_t i = 0;
for (; i < 20; ++i)
{
printf ("Deputy no: %zu before f().\n", i);
pthread_create (&ptid[i], NULL, deputy, &ptid[i]);
}
}
/* Make sure main does not end before all threads ended,
as they make use of ptid[] which lives on main's stack. */
{
size_t i = 0;
for (; i < 20; ++i)
{
pthread_join(ptid[i]);
}
}
}
另请注意,当传递pthread_t
类型变量的地址时,线程函数也应该使用它,如下所示:
void *
deputy (void *arg)
{
pthread_t ptid = *((pthread_t *) arg);
...
^ 2 pthread_t
也是不透明的。所以它的表示是实现定义的。如果可以是整数或指针或... - 那么如果依赖于所使用的PThread库的特定实现,如何打印。