gpt4 book ai didi

c++ - 为文件写入实现互斥锁

转载 作者:行者123 更新时间:2023-11-30 21:06:22 25 4
gpt4 key购买 nike

我尝试使用互斥体来避免对 C/Cpp 中的同一线程进行多次写入。下面是我的程序的流程。我很困惑在哪里包含我的锁定和解锁代码。

main() {
spawn a worker thread
}
worker_thread() {
read the input file name
read some content
write the content to the given file name
}

我看到的大多数实现似乎都是这样的:

main() {
pthread_mutex_init(&myMutex;,0);
*spawn a worker thread*
pthread_join(thread1, 0);
pthread_mutex_destroy(&myMutex;);
}
worker_thread() {
read the input file name
read some content
write the content to the given file name
}

我想要的是这样的:

main() {
spawn a worker thread
}
worker_thread() {
read the input file name
read some content
pthread_mutex_init(&myMutex;,0) --> for the given file?
write the content to the given file name
pthread_mutex_destroy(&myMutex;);
}

任何继续进行的想法都非常感谢。谢谢!

最佳答案

为 iostream 创建一个包装器非常容易,以确保一次只有一个线程可以写入流。不幸的是,几乎一旦你这样做了,你就会遇到另一个问题。它确保一次只有一个线程可以插入流中,因此您可以获得定义的行为。但是,如果您有类似的情况:

线程 1:sync_stream << a << b << c << '\n';
线程 2:sync_stream << x << y << z << '\n';

您想要的是:

abc
xyz

...否则:

xyz
abc

由于它们位于单独的线程中,因此它们之间的顺序可以改变,但一个线程的一行输出应保持为单行输出。像这样的东西:

abxy
cz

...可能不希望或 Not Acceptable 。为了确保避免这种情况,我们确实需要两个单独的类。一种是同步流。另一个是让我们将一些(或多或少任意的)插入流作为单个不可分割的“事务”。为此,我们可以使用一对这样的类:

class transaction {
std::ostringstream buffer;
public:
transaction(std::string const &s="") : buffer(s, std::ios::out | std::ios::ate) {}

template <class T>
transaction &operator<<(T const &t) {
buffer << t;
return *this;
}

friend std::ostream &operator<<(std::ostream &os, transaction const &t) {
return os << t.buffer.str();
}
};

class sync_stream {
std::ostream &out;
std::mutex mutex;
public:
sync_stream(std::ostream &sink) : out(sink) { }

void operator<<(transaction const &t) {
std::lock_guard<std::mutex> l(mutex);
out << t;
}
};

请注意 transaction类支持链接,但 sync_stream不(并且您唯一可以插入其中的是 transaction )。要使用它们,我们执行以下操作:

for (int i=0; i<10; i++)
threads[i] = std::thread([&]{
for (int i=0; i<10; i++)
s << (transaction() << "Thread: " << std::this_thread::get_id() << "\n");
});

这样,线程认为是单个输出的内容实际上会作为单个输出出现,因此我们的结果可能如下所示:

Thread: 140375947724544
Thread: 140376068564736
Thread: 140375964509952
Thread: 140375964509952
Thread: 140375972902656
Thread: 140375964509952

当然,您会得到与我不同的线程 ID,并且行的顺序可能会有所不同 - 但每一行都将被写为一个完整的单元。

摘要

工作线程根本不应该直接使用互斥体。这应该是自动化的,以便工作线程可以专注于其工作,并且只在完成其工作所需的底层机制上花费最少的精力。

关于c++ - 为文件写入实现互斥锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49155497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com