我需要针对一个数据竞争场景和正确同步的解决方案。
struct download_info {
int download_id;
notification_data noti_data;
};
download_info* global_info;
// Will be called from download_module
void download_progress_callback(int download_id, int progress_val) {
global_info->current_val = progress_val;
update_notification(global_info);
}
// will be called from notification_module
notification_timeout_callback() {
// Here I need to modify the notification_look
// [1]. So I will delete the noti_data
// [2]. create a new noti_data just to that of existing one with same notification_id
// [3]. update the look of notification
update_notification(global_info);
}
这两个功能都存在竞争条件和同步。
现在克服这种竞争条件&同步,我可以使用pthread_mutex_t
。但是我在使用它时遇到两个问题。
download_progress_callback()
过于频繁地被调用,所以
会有很多等待。 pthread_mutex_trylock()
,可能会出现无法获得进一步进度通知的情况,并且无法知道下载是否已完成。处理此案件的任何建议。我已经截了很多细节,所以如果有什么不清楚,请询问。