我正在實現一種算法,其中我已經使用了CPP和Intel TBB庫的MultiCore線程庫。我在線程的幫助下調用了函數,有時它會完美執行,有時它會運行時間異常。我試圖找到原因,但無法找到。請查找提供異常的代碼片段示例。在代碼中引發運行時異常
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <tbb/pipeline.h>
#include <tbb/atomic.h>
#include <tbb/concurrent_queue.h>
#include <tbb/compat/thread>
#include <tbb/tbbmalloc_proxy.h>
using namespace std;
using namespace tbb;
#define pi 3.141593
#define FILTER_LEN 265
class MyBuffer
{
public:
double *acc;
double *buffer;
int start,end;
MyBuffer()
{
start=0;
end=0;
buffer=new double[150264];
acc=new double[150000];
fill_n(buffer,150264,0);
}
~MyBuffer()
{
delete[] buffer;
delete[] acc;
}
int startnumber()
{
return start;
}
int endnumber()
{
return end;
}
};
typedef concurrent_queue<MyBuffer> QueueMyBufferType;
QueueMyBufferType chunk_queue;
atomic<bool> stop_flag;
// input function that will be running by thread to generate sinewave
void input_function()
{
stop_flag = false;
cout<<"thread reached to call input function " <<endl;
ofstream o("testing sinewave.csv");
int counter=0;
while(counter<150000)
{
cout<<"value of counter is \t" <<counter << endl;
//MyBuffer *b=new MyBuffer;
MyBuffer b;
b.start=(FILTER_LEN-1+(counter));
b.end=(25264+(counter));
cout<<"value of b.start is and b.end is "<<b.start<<"\t" <<b.end<<endl;
for(int i =b.startnumber(); i <b.endnumber(); i++)
{
b.buffer[i] = sin(700 * (2 * pi) * (i/5000.0));
o<<b.buffer[i]<<endl;
}
chunk_queue.push(b);
cout<<"object pushed in queue and value of j is \t" <<counter <<endl;
counter+=25000;
}
stop_flag = true;
cout<<"all data is perfectly generated" <<endl;
}
int main()
{
thread input_thread(input_function);
while(!stop_flag)
{
//cout<<"waiting for thread " << endl;
}
cout << "\n All Data is processed \n\n" << endl;
return 0;
}
這段代碼是應用程序的一部分,併產生正弦波。請幫忙尋找在那裏我得到錯誤的幫助。
其運行時異常它給? – NetStarter
*「...有時它完美執行,有時它會使運行時異常...」*。數據競爭的一個可能的症狀,因此將落入*未定義的行爲*土地。 –
@NetStarter在控制檯上打印後,它給這個exe文件的所有輸出已停止工作.... –