
这个使用CreateThread创建线程写多线程的程序怎么不对啊,求解,Win32小程序
#include<stdlib.h>#include<iostream>#include<WinBase.h>usingnamespacestd;voidcal1(int...
#include <stdlib.h>
#include <iostream>
#include <WinBase.h>
using namespace std;
void cal1(int);
void cal2(int);
unsigned long long themax, themin;
unsigned long long themax1, themin1;//主线程
unsigned long long themax2, themin2;//副线程
int main()
{
int times1 = 2000000;
int times2 = 3000000;
HANDLE FstThread = 0, SecThread = 0, ArrayofHandle[2];
FstThread = CreateThread(NULL, 0, cal1, ×1, 0, NULL);
SecThread = CreateThread(NULL, 0, cal2, ×2, 0, NULL);
cout << "The Max:" << (themax1 > themax2 ? themax1 : themax2) << endl
<< "The Min:" << (themin = themin1 > themin2 ? themin1 : themin2) << endl;
ArrayofHandle[0] = FstThread;
ArrayofHandle[1] = SecThread;
WaitForMultipleObjects(2, ArrayofHandle, true, INFINITE);
CloseHandle(FstThread);
CloseHandle(SecThread);
return 0;
}
void cal1(int CalTimes)
{
unsigned long long temp;
for (unsigned i = 0; i < CalTimes; i++)
{
srand(__rdtsc());
temp = rand();
if (temp > themax1)
themax1 = temp;
themin1 = themax1;
if (temp < themin1)
themin1 = themax1;
}
}
void cal2(int CalTimes)
{
unsigned long long temp;
for (unsigned i = 0; i < CalTimes; i++)
{
srand(__rdtsc());
temp = rand();
if (temp > themax2)
themax2 = temp;
themin2 = themax2;
if (temp < themin2)
themin2 = themax2;
}
}
大概就是用tsc做种子得到大量随机数,然后求其中一个最大的,一个最小的。这电脑CPU不怎么行,所以我想试试多线程的办法。程序参照网上的一个例子写的,结果会报很多行错误,都是如下的:
1>c:\program files (x86)\windows kits\8.1\include\um\minwinbase.h(251): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
字数上限了,简单说,我就是想要一个如上功能的程序,能利用CPU更多的时间片更快地得到结果 展开
#include <iostream>
#include <WinBase.h>
using namespace std;
void cal1(int);
void cal2(int);
unsigned long long themax, themin;
unsigned long long themax1, themin1;//主线程
unsigned long long themax2, themin2;//副线程
int main()
{
int times1 = 2000000;
int times2 = 3000000;
HANDLE FstThread = 0, SecThread = 0, ArrayofHandle[2];
FstThread = CreateThread(NULL, 0, cal1, ×1, 0, NULL);
SecThread = CreateThread(NULL, 0, cal2, ×2, 0, NULL);
cout << "The Max:" << (themax1 > themax2 ? themax1 : themax2) << endl
<< "The Min:" << (themin = themin1 > themin2 ? themin1 : themin2) << endl;
ArrayofHandle[0] = FstThread;
ArrayofHandle[1] = SecThread;
WaitForMultipleObjects(2, ArrayofHandle, true, INFINITE);
CloseHandle(FstThread);
CloseHandle(SecThread);
return 0;
}
void cal1(int CalTimes)
{
unsigned long long temp;
for (unsigned i = 0; i < CalTimes; i++)
{
srand(__rdtsc());
temp = rand();
if (temp > themax1)
themax1 = temp;
themin1 = themax1;
if (temp < themin1)
themin1 = themax1;
}
}
void cal2(int CalTimes)
{
unsigned long long temp;
for (unsigned i = 0; i < CalTimes; i++)
{
srand(__rdtsc());
temp = rand();
if (temp > themax2)
themax2 = temp;
themin2 = themax2;
if (temp < themin2)
themin2 = themax2;
}
}
大概就是用tsc做种子得到大量随机数,然后求其中一个最大的,一个最小的。这电脑CPU不怎么行,所以我想试试多线程的办法。程序参照网上的一个例子写的,结果会报很多行错误,都是如下的:
1>c:\program files (x86)\windows kits\8.1\include\um\minwinbase.h(251): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
字数上限了,简单说,我就是想要一个如上功能的程序,能利用CPU更多的时间片更快地得到结果 展开
展开全部
这代码是不是从linux平台上直接复制过来的?因为windows平台的64位大整数用的一个关键字__int64,而使用unsigned long long的好像是Linux .
其次,你的线程函数,在Windows平台上是如下的函数原型格式 :
DWORD ThreadFuncName(arg list); //线程函数的返回值不对
再次,我没有编译通过~_~ 可能是编程环境的事,因为一直用的vc6.0 还么有vs...不过,那个传递给线程函数的参数 x1 x2 你得改一下。。
其次,你的线程函数,在Windows平台上是如下的函数原型格式 :
DWORD ThreadFuncName(arg list); //线程函数的返回值不对
再次,我没有编译通过~_~ 可能是编程环境的事,因为一直用的vc6.0 还么有vs...不过,那个传递给线程函数的参数 x1 x2 你得改一下。。
追问
这个是我写的第一个程序呢。后来我发现问题出现在Winbase.h,我上网查例子,发现很多用的都是Windows.h,我改成这个头文件,华丽丽地通过并可以运行了。那个变量声明确实太冗长了,求随机数的话不需要负的,所以改成了UINT64
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询