gpt4 book ai didi

c++ - 如何为 c++11 更正以下生产者消费者代码

转载 作者:行者123 更新时间:2023-11-28 03:32:14 25 4
gpt4 key购买 nike

我正在为 c++11 编写一个带有简单信号量实现的消费者/生产者类。但是,以下代码无法编译。如果我删除 producer_consumer 类并制作 producerconsumer 全局函数,并移动线程创建部分 (std::thread t1( consumer), t2(consumer); t1.join(); t2.join();)到main函数,编译通过了,但是执行还是不正确,最终会导致进入段错误。我怎样才能更正代码?谢谢。

#include <iostream>
#include <thread>
#include <mutex>

class semaphore {
private:
std::mutex m;
std::condition_variable cond;
unsigned long n;
public:
semaphore(int i) : n(i) {}

void up() {
std::unique_lock <std::mutex> lock (m);
++n;
cond.notify_one();
}

void down() {
std::unique_lock <std::mutex> lock (m);
while(!n) cond.wait(lock);
--n;
}
};
class producer_consumer{
private:
semaphore full, empty;
int i = 0;
std::thread t1, t2;
public:
producer_consumer(int n): full(0), empty(n), i(0){}
void run(){
t1 = std::thread(&producer_consumer::producer, *this);
t2 = std::thread(&producer_consumer::consumer, *this);
}
void stop(){
t1.join();
t2.join();
}
void producer (){
while (true){
empty.down();
i ++;
std::cout << "[p]" << i << std::endl;
full.up();
}
}
void consumer (){
while (true){
full.down();
i --;
std::cout << "[c]" << i << std::endl;
empty.up();
}
}
};

int main(){
producer_consumer pc(5);
pc.run();
pc.stop();
return 0;
}

我使用 clang++ 编译文件:

clang++ -std=c++0x -stdlib=libc++ pc.cpp ; ./a.out

错误信息:

In file included from file_name.cpp:1:
In file included from /usr/bin/../lib/c++/v1/iostream:38:
In file included from /usr/bin/../lib/c++/v1/ios:216:
In file included from /usr/bin/../lib/c++/v1/__locale:15:
In file included from /usr/bin/../lib/c++/v1/string:434:
In file included from /usr/bin/../lib/c++/v1/algorithm:591:
/usr/bin/../lib/c++/v1/type_traits:1423:12: error: call to implicitly-deleted
copy constructor of 'typename decay<producer_consumer &>::type'
(aka 'producer_consumer')
return _VSTD::forward<_Tp>(__t);
^~~~~~~~~~~~~~~~~~~~~~~~

更新:@DanqiWang 通过将*this 更改为this 解决了编译问题。现在看来是信号量不正常,最后会导致程序崩溃:

./a.out
[p]1
[p]2
[p]3
[p]4
[p]5
....
[p]2
[p]3
[p]4
1
[c]3
[p[]c3]
3
[[c]3
p][2c
][p]3
[p]4
4
[c]3
[c]2
Segmentation fault: 11

最佳答案

我对您的代码有以下建议:

  • 虽然 iostream 在 C++11 中成为线程安全的,但它应该被锁定以避免输出错误。
  • 使用 std::atomic 而不是裸 int
  • 如果存在段错误,转储核心,使用 gdb 对其进行调试,或在此处发布堆栈跟踪。

希望这些帮助。

关于c++ - 如何为 c++11 更正以下生产者消费者代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12189782/

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