我有一个 MyLib.h
typedef void *MyThread;
和一个 MyLib.c,它有:
typedef struct
{
ucontext_t *context;
int tid;
}_MyThread;
有一个测试函数可以创建一个线程并发出一个连接:
void f0(void * dummy)
{
MyThread t1;
printf("f0 start\n");
t1 = MyThreadCreate(f1, NULL);
_MyThread *t = (_MyThread*)t1;
printf("passed value=%d\n",t->tid);
printf("f0 join on t1\n");
MyThreadJoin(t1);
....
}
MyLib.c 中的 MyThreadCreate 和 MyThreadJoin 如下:
MyThread MyThreadCreate(void(*start_funct)(void *), void *args)
{
_MyThread child_thread;
...
//setting the child thread's tid
child_thread.tid = ++active_threads;
... MyThread ret = (MyThread)&child_thread;
return ret;
}
int MyThreadJoin(MyThread thread)
{
_MyThread *arg_thread;
arg_thread = (_MyThread*)thread;
int id = arg_thread->tid;
....
}
我的问题是,当我运行上述内容时,我得到:
passed value=2
f0 join on t1
arg_thread->tid = 13
传递的值 = "2" 是正确的,但是库函数中出现的值 "13" 是错误的。为什么以相同的方式取消引用传递的值与调用函数不同而在被调用函数中不同?