Arrays 累计+;1/-1 cointost在1000次迭代中崩溃。请告知;c++;boost随机库
遵循以前的一些建议 我想我有一个使用boost的线程安全数字生成器,但是当我输入1000次迭代时,我的程序崩溃了。绘制图形时的输出.csv文件看起来不错,但我不确定它为什么会崩溃 它使用的是beginthread,每个人都告诉我应该使用更复杂的ReadEx,这是我不熟悉的。如果有人能推荐一个例子,我将不胜感激 还有。。。有人指出,我应该在我的_beginthread上应用第二个参数来计算数组的开始位置,但除了尝试使用结构之外,我不知道如何传递多个参数,而且我已经阅读了结构,并且_beginthread没有进展(尽管我可以使用boost线程…)Arrays 累计+;1/-1 cointost在1000次迭代中崩溃。请告知;c++;boost随机库,arrays,multithreading,boost,Arrays,Multithreading,Boost,遵循以前的一些建议 我想我有一个使用boost的线程安全数字生成器,但是当我输入1000次迭代时,我的程序崩溃了。绘制图形时的输出.csv文件看起来不错,但我不确定它为什么会崩溃 它使用的是beginthread,每个人都告诉我应该使用更复杂的ReadEx,这是我不熟悉的。如果有人能推荐一个例子,我将不胜感激 还有。。。有人指出,我应该在我的_beginthread上应用第二个参数来计算数组的开始位置,但除了尝试使用结构之外,我不知道如何传递多个参数,而且我已经阅读了结构,并且_beginth
#包括
#包括
#包括
#包括
#包括
#包括
#包括
//对于srand48_r(时间(NULL),&randBuffer);这不管用
#包括
#包括
//#包括
使用名称空间std;
使用名称空间boost;
使用名称空间boost::random;
void myThread0(void*dummy);
void myThread1(void*dummy);
void myThread2(void*dummy);
void myThread3(void*dummy);
//随机种子
void初始化();
//从https://stackoverflow.com/questions/7114043/random-number-generation-in-c11-how-to-generate-how-do-they-work
均匀分布二(1,2);
typedef std::mt19937 MyRNG;//Mersenne捻线机具有广泛的参数选择
uint32\u t seed\u val;//以某种方式填充
MyRNG rng1;//e、 g.保留一个全局实例(每个线程)
MyRNG rng2;//e、 g.保留一个全局实例(每个线程)
MyRNG rng3;//e、 g.保留一个全局实例(每个线程)
MyRNG rng4;//e、 g.保留一个全局实例(每个线程)
//只有共享变量才需要
//临界截面cs1、cs2、cs3、cs4;//全球的
int main()
{
流文件;
myfile.open(“cointost.csv”);
内部;
长纽伦;
长计数=0;
整数除数=1;
浮动fHolder=0;
长计数器=0;
浮动百分比=0.0;
//?
//无符号threadID;
//处理hThread;
初始化();
句柄hThread[4];
常数int size=100000;
int数组[大小];
printf(“运行次数(使用100000的倍数)”;
cin>>numRuns;
对于(int a=0;a //cout这并不是问题的确切答案(我确实读到过_beginThread容易发生内存泄漏),但我确实创建了一个只使用boost线程的v5(删除了注释行)。顺便说一句,我不知道它在哪里崩溃
#include <process.h>
#include <windows.h>
#include <iostream>
#include <fstream>
#include <time.h>
#include <random>
#include <boost/thread.hpp>
#include <boost/random.hpp>
//for srand48_r(time(NULL), &randBuffer); which doesn't work
#include <stdio.h>
#include <stdlib.h>
//#include <thread>
using namespace std;
using namespace boost;
using namespace boost::random;
//for random seeds
void initialize();
void workerFunc1(void *dummy );
void workerFunc2(void *dummy );
void workerFunc3(void *dummy );
void workerFunc4(void *dummy );
//from http://stackoverflow.com/questions/7114043/random-number-generation-in-c11-how-to-generate-how-do-they-work
uniform_int_distribution<> two(1,2);
typedef std::mt19937 MyRNG; // the Mersenne Twister with a popular choice of parameters
uint32_t seed_val; // populate somehow
MyRNG rng1; // e.g. keep one global instance (per thread)
MyRNG rng2; // e.g. keep one global instance (per thread)
MyRNG rng3; // e.g. keep one global instance (per thread)
MyRNG rng4; // e.g. keep one global instance (per thread)
//only needed for shared variables
//CRITICAL_SECTION cs1,cs2,cs3,cs4; // global
int main()
{
ofstream myfile;
myfile.open ("coinToss.csv");
int rNum;
long numRuns;
long count = 0;
int divisor = 1;
float fHolder = 0;
long counter = 0;
float percent = 0.0;
//?
//unsigned threadID;
//HANDLE hThread;
initialize();
HANDLE hThread[4];
const int size = 100000;
int array[size];
printf ("Runs (uses multiple of 100,000) ");
cin >> numRuns;
for (int a = 0; a < numRuns; a++)
{
thread workerThread1(workerFunc1, (void*)(array) );
thread workerThread2(workerFunc2, (void*)(array) );
thread workerThread3(workerFunc3, (void*)(array) );
thread workerThread4(workerFunc4, (void*)(array) );
//waits for threads to finish before continuing
workerThread1.join();
workerThread2.join();
workerThread3.join();
workerThread4.join();
//dump array into calculations
//average array into fHolder
//this could be split into threads as well
for (int p = 0; p < size; p++)
{
counter += array[p] == 2 ? 1 : -1;
//cout << array[p] << endl;
//cout << counter << endl;
}
//this fHolder calculation didn't work
//fHolder = counter / size;
//so I had to use this
//cout << counter << endl;
fHolder = counter;
fHolder = fHolder / size;
myfile << fHolder << endl;
}
}
void initialize()
{
//seed value needs to be supplied
//rng1.seed(seed_val*1);
rng1.seed((unsigned int)time(NULL));
rng2.seed(((unsigned int)time(NULL))*2);
rng3.seed(((unsigned int)time(NULL))*3);
rng4.seed(((unsigned int)time(NULL))*4);
};
//recieves array, and spot of array to work on,
void workerFunc1(void *param)
{
int *i = (int *)param;
for (int x = 0; x < 25000; x++)
{
//gets a random 1 or 2 value
i[x] = two(rng1);
//converts to -1 or +1, then adds it to sum (I would love to combine these two)
}
//return sum;
}
void workerFunc2(void *param)
{
int *i = (int *)param;
for (int x = 25000; x < 50000; x++)
{
//gets a random 1 or 2 value
i[x] = two(rng2);
//converts to -1 or +1, then adds it to sum (I would love to combine these two)
}
//return sum;
}
void workerFunc3(void *param)
{
int *i = (int *)param;
for (int x = 50000; x < 75000; x++)
{
//gets a random 1 or 2 value
i[x] = two(rng3);
//converts to -1 or +1, then adds it to sum (I would love to combine these two)
}
//return sum;
}
void workerFunc4(void *param)
{
int *i = (int *)param;
for (int x = 75000; x < 100000; x++)
{
//gets a random 1 or 2 value
i[x] = two(rng4);
//converts to -1 or +1, then adds it to sum (I would love to combine these two)
}
//return sum;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
//对于srand48_r(time(NULL),&randBuffer);这不起作用
#包括
#包括
//#包括
使用名称空间std;
使用名称空间boost;
使用名称空间boost::random;
//随机种子
void初始化();
无效的workerFunc1(无效*虚拟);
无效workerFunc2(无效*虚拟);
无效的workerFunc3(无效*虚拟);
无效的workerFunc4(无效*虚拟);
//从http://stackoverflow.com/questions/7114043/random-number-generation-in-c11-how-to-generate-how-do-they-work
均匀分布二(1,2);
typedef std::mt19937 MyRNG;//带有常用参数选择的Mersenne捻线器
uint32\u t seed\u val;//以某种方式填充
MyRNG rng1;//例如,保留一个全局实例(每个线程)
MyRNG rng2;//例如,保留一个全局实例(每个线程)
MyRNG rng3;//例如,保留一个全局实例(每个线程)
MyRNG rng4;//例如,保留一个全局实例(每个线程)
//只有共享变量才需要
//临界截面cs1、cs2、cs3、cs4;//全局
int main()
{
流文件;
myfile.open(“cointost.csv”);
内部;
长纽伦;
长计数=0;
整数除数=1;
浮动fHolder=0;
长计数器=0;
浮动百分比=0.0;
//?
//无符号threadID;
//处理hThread;
初始化();
句柄hThread[4];
常数int size=100000;
int数组[大小];
printf(“运行次数(使用100000的倍数)”;
cin>>numRuns;
对于(int a=0;a //你能不能把这段代码简化成最短的完整代码段
#include <process.h>
#include <windows.h>
#include <iostream>
#include <fstream>
#include <time.h>
#include <random>
#include <boost/thread.hpp>
#include <boost/random.hpp>
//for srand48_r(time(NULL), &randBuffer); which doesn't work
#include <stdio.h>
#include <stdlib.h>
//#include <thread>
using namespace std;
using namespace boost;
using namespace boost::random;
//for random seeds
void initialize();
void workerFunc1(void *dummy );
void workerFunc2(void *dummy );
void workerFunc3(void *dummy );
void workerFunc4(void *dummy );
//from http://stackoverflow.com/questions/7114043/random-number-generation-in-c11-how-to-generate-how-do-they-work
uniform_int_distribution<> two(1,2);
typedef std::mt19937 MyRNG; // the Mersenne Twister with a popular choice of parameters
uint32_t seed_val; // populate somehow
MyRNG rng1; // e.g. keep one global instance (per thread)
MyRNG rng2; // e.g. keep one global instance (per thread)
MyRNG rng3; // e.g. keep one global instance (per thread)
MyRNG rng4; // e.g. keep one global instance (per thread)
//only needed for shared variables
//CRITICAL_SECTION cs1,cs2,cs3,cs4; // global
int main()
{
ofstream myfile;
myfile.open ("coinToss.csv");
int rNum;
long numRuns;
long count = 0;
int divisor = 1;
float fHolder = 0;
long counter = 0;
float percent = 0.0;
//?
//unsigned threadID;
//HANDLE hThread;
initialize();
HANDLE hThread[4];
const int size = 100000;
int array[size];
printf ("Runs (uses multiple of 100,000) ");
cin >> numRuns;
for (int a = 0; a < numRuns; a++)
{
thread workerThread1(workerFunc1, (void*)(array) );
thread workerThread2(workerFunc2, (void*)(array) );
thread workerThread3(workerFunc3, (void*)(array) );
thread workerThread4(workerFunc4, (void*)(array) );
//waits for threads to finish before continuing
workerThread1.join();
workerThread2.join();
workerThread3.join();
workerThread4.join();
//dump array into calculations
//average array into fHolder
//this could be split into threads as well
for (int p = 0; p < size; p++)
{
counter += array[p] == 2 ? 1 : -1;
//cout << array[p] << endl;
//cout << counter << endl;
}
//this fHolder calculation didn't work
//fHolder = counter / size;
//so I had to use this
//cout << counter << endl;
fHolder = counter;
fHolder = fHolder / size;
myfile << fHolder << endl;
}
}
void initialize()
{
//seed value needs to be supplied
//rng1.seed(seed_val*1);
rng1.seed((unsigned int)time(NULL));
rng2.seed(((unsigned int)time(NULL))*2);
rng3.seed(((unsigned int)time(NULL))*3);
rng4.seed(((unsigned int)time(NULL))*4);
};
//recieves array, and spot of array to work on,
void workerFunc1(void *param)
{
int *i = (int *)param;
for (int x = 0; x < 25000; x++)
{
//gets a random 1 or 2 value
i[x] = two(rng1);
//converts to -1 or +1, then adds it to sum (I would love to combine these two)
}
//return sum;
}
void workerFunc2(void *param)
{
int *i = (int *)param;
for (int x = 25000; x < 50000; x++)
{
//gets a random 1 or 2 value
i[x] = two(rng2);
//converts to -1 or +1, then adds it to sum (I would love to combine these two)
}
//return sum;
}
void workerFunc3(void *param)
{
int *i = (int *)param;
for (int x = 50000; x < 75000; x++)
{
//gets a random 1 or 2 value
i[x] = two(rng3);
//converts to -1 or +1, then adds it to sum (I would love to combine these two)
}
//return sum;
}
void workerFunc4(void *param)
{
int *i = (int *)param;
for (int x = 75000; x < 100000; x++)
{
//gets a random 1 or 2 value
i[x] = two(rng4);
//converts to -1 or +1, then adds it to sum (I would love to combine these two)
}
//return sum;
}