【发布时间】:2010-11-14 17:56:24
【问题描述】:
我有一个在线程中使用的全局结构,我使用pthread.h 创建它。
其中有并发线程事件更新变量。
因此,我在结构中的数据经常不同步,正如我通过大量调试发现的那样。
C afaik 中没有像“synchonized”(Java 背景)这样的关键字。那么如何阻止数据结构以使其保持有效呢?
我的结构是:
struct thread_data
{
int nr;
int time;
};
struct thread_data thread_data_array[MAX_THR];
MAX_THR 定义为 10,例如 ..
我的数据通过pthread_create 传递到新线程:
pthread_create(&threads[num_threads], NULL, Thread_Fkt, &thread_data_array[num_thread]);
有时线程是在其他线程中创建的。没有线性终止。 C 是否提供了一组同步的数据结构?
谢谢 ;)
【问题讨论】:
标签: c linux multithreading posix