C++ 引发异常:调用方法front时发生读取访问冲突
当我试图从队列中取出第一个项目时,出现了错误 引发异常:读取访问冲突。 **std::deque::front**(…)返回0xDD9 发生错误时,队列不是空的C++ 引发异常:调用方法front时发生读取访问冲突,c++,C++,当我试图从队列中取出第一个项目时,出现了错误 引发异常:读取访问冲突。 **std::deque::front**(…)返回0xDD9 发生错误时,队列不是空的 #include <iostream> #include <queue> #include <thread> #include<mutex> #include<chrono> using namespace std; int total; queue<int> ox
#include <iostream>
#include <queue>
#include <thread>
#include<mutex>
#include<chrono>
using namespace std;
int total;
queue<int> ox;
mutex mtx, mtx1;
void push(int x) {
mtx1.lock();
ox.push(x);
mtx1.unlock();
}
bool pop(int x) {
if (ox.size() == 0) this_thread::sleep_for(chrono::milliseconds(1));
if (ox.size() != 0) {
mtx.lock();
x = ox.front();
ox.pop();
mtx.unlock();
return true;
}
if (ox.size() == 0) {
return false;
}
return false;
}
void producer() {
for (int i = 0; i < 1024 * 1024 * 4; i++) push(1);
}
void consumer() {
int sum = 0;
for (int i = 0; i < 1024 * 1024 * 4; i++) {
int k;
if (pop(k)) sum += k;
}
total += sum;
}
int main()
{
thread th1(producer);
thread th2(consumer);
th1.join();
th2.join();
cout << total;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
整数合计;
排队牛;
互斥mtx,mtx1;
无效推送(整数x){
mtx1.lock();
牛推(x);
mtx1.unlock();
}
布尔波普(整数x){
如果(ox.size()==0)此线程::睡眠时间为(chrono::毫秒(1));
如果(箱尺寸()!=0){
mtx.lock();
x=前x轴();
牛瘟();
mtx.unlock();
返回true;
}
如果(ox.size()==0){
返回false;
}
返回false;
}
无效生产者(){
对于(inti=0;i<1024*1024*4;i++)推送(1);
}
无效消费者(){
整数和=0;
对于(int i=0;i<1024*1024*4;i++){
int k;
如果(pop(k))和+=k;
}
总数+=总和;
}
int main()
{
螺纹th1(生产者);
螺纹th2(耗电元件);
th1.join();
th2.join();
可能引用变量使用错误。请将其替换为指针。\include
#include <iostream>
#include <queue>
#include <thread>
#include<mutex>
#include<chrono>
using namespace std;
long long total;
queue<int> ox;
mutex mtx;
void push(int x) {
mtx.lock();
ox.push(x);
mtx.unlock();
}
bool pop(int& x) {
mtx.lock();
if (ox.size() == 0) this_thread::sleep_for(chrono::milliseconds(1));
if (ox.size() != 0) {
x = ox.front();
ox.pop();
mtx.unlock();
return true;
}
mtx.unlock();
return false;
}
void producer() {
for (int i = 0; i < 1024 * 1024 * 4; i++) push(1);
}
void consumer() {
for (int i = 0; i < 1024 * 1024 * 4; i++) {
int k;
if (pop(k)) total += k;
}
}
int main()
{
thread th1(producer);
thread th2(consumer);
th1.join();
th2.join();
cout << total;
}
#包括
#包括
#包括
#包括
使用名称空间std;
长总;
排队牛;
互斥mtx;
无效推送(整数x){
mtx.lock();
牛推(x);
mtx.unlock();
}
布尔波普(int&x){
mtx.lock();
如果(ox.size()==0)此线程::睡眠时间为(chrono::毫秒(1));
如果(箱尺寸()!=0){
x=前x轴();
牛瘟();
mtx.unlock();
返回true;
}
mtx.unlock();
返回false;
}
无效生产者(){
对于(inti=0;i<1024*1024*4;i++)推送(1);
}
无效消费者(){
对于(int i=0;i<1024*1024*4;i++){
int k;
如果(pop(k))总+=k;
}
}
int main()
{
螺纹th1(生产者);
螺纹th2(耗电元件);
th1.join();
th2.join();
cout每个线程都有一个单独的std::mutex
,这意味着实际上根本没有互斥体。两个线程都需要使用相同的互斥体
您的pop
函数中也有输入错误,它应该是boolpop(int&x)
您不应该直接调用mtx.lock()
和mtx.unlock()
,最好使用std::unique\u lock
(或标准库中定义的其他锁之一),以确保互斥锁在超出范围时始终处于解锁状态,例如:
void push(int x) {
std::unique_lock<std::mutex> lock(mtx);
ox.push(x);
}
bool pop(int& x) {
if (ox.size() == 0) this_thread::sleep_for(chrono::milliseconds(1));
if (ox.size() != 0) {
std::unique_lock<std::mutex> lock(mtx);
x = ox.front();
ox.pop();
return true;
}
if (ox.size() == 0) {
return false;
}
return false;
}
无效推送(int x){
std::唯一锁(mtx);
牛推(x);
}
布尔波普(int&x){
如果(ox.size()==0)此线程::睡眠时间为(chrono::毫秒(1));
如果(箱尺寸()!=0){
std::唯一锁(mtx);
x=前x轴();
牛瘟();
返回true;
}
如果(ox.size()==0){
返回false;
}
返回false;
}
请提供。0xdddddd
是visual studio用来表示释放的堆内存的。猜测一下,ox
是对象的一个成员,并且该对象已被删除。请提供一个选项,您的编辑现在仍然不是一个匹配的选项。为什么要为推送和弹出使用两个不同的互斥锁?这有什么意义?当查询队列大小。@Harry严格地说是的,但我认为在这种情况下,它不需要