如何解决我们可以使用 std::atomic 使 S[x++] = str 原子化吗?
我有以下单例类:
big_list_dictionary
我想从多个线程执行以下操作:
big_list_dictionary = [{'name': 'test = 1','id': 1,'value': 30},{'name': 'apple = 1','id': 2,'value': 70},{'name': 'orange = 1','id': 3,'value': 10},{'name': 'balloon = 1','id': 4,'value': 20},{'name': 'airplane = 1','id': 5,'value': 40}]
total1 = [{ 'name': 'test','total': 130},{'name': 'apple','total': 270},{'name': 'orange','total': 310},{'name': 'balloon','total': 420},{'name': 'airplane','total': 540}]
total2 = [{'name': 'test','total': 230},'total': 570},'total': 3210},'total': 620},'total': 940}]
intermediate = {i['name'].split('=')[0].strip():i for i in big_list_dictionary}
for t1 in total1:
if t1['name'] not in intermediate:
temp_dict = {'name': t1['name'],'id': 0,'value': 0}
big_list_dictionary.append(temp_dict)
intermediate[t1['name']] = temp_dict
# insert rest of the answer from code above
我可以使用 class RecordKeeper {
public:
...
RecordKeeper* get();
void addRecord(std::string &rec);
private:
std::string records[MAX]; // this has to be a fixed size circular array
int len;
};
void RecordKeeper::addRecord(std::string rec) {
...
records[len++] = rec;
...
}
将其作为原子操作进行吗?
目前我正在使用 std::mutex 和 lock_guard,例如 RecordKeeper::get()->addRecord(str);
。但是有没有更好的方法可以更高效、更快?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。