- 论坛徽章:
- 0
|
我创建了一个线程,主要是用来与内核用 netlink 相互通消息,收到某消息,子线程会在一个static 数组里对某位赋0或者1的值。在子进程里打印的话,这个数组里的值都是对的。
但是在子线程外面,用别的函数来读这个static 数组的时候,返回的值就是全0. 因为别的函数来读的频率很慢,所以没有用 pthread_mutex_lock 来做保护。
为什么别的函数来读这个全局变量,不能读到和子线程一样的值呢?
static int p_portstate[6];
int main_loop_back(void)
{
pthread_t tid;
int error;
error=pthread_create(&tid,NULL,loop_thread_fun,NULL);
if(error!=0)
{
printf("thread is not created...\n");
return -1;
}
printf("create thread succes, main pid is %d\n",getpid());
return 0;
}
// 子线程的代码可以视为:
viod loop_thread_fun()
{
//从内核得到值;
。。。。。
//赋值
p_portstate[i] = 1;
}
然后别的函数调用,就是直接使用p_portstate数组里的值,发现打印出来是全0.。。。。。。。
请各位赐教!!
|
|