我正在创建一个两线程数组求和程序,我正在使用 windows.h 线程。这是我到目前为止的代码。
#include "StdAfx.h"
#include <stdio.h>
#include <iostream>
#include <windows.h>
#include <process.h> // needed for _beginthread()
void silly( void * ); // function prototype
using namespace std;
int arraySum[100];
int main()
{
// Our program's first thread starts in the main() function.
printf( "Now in the main() function.\n" );
for(int i = 0 ; i < 100 ; i++){
arraySum[i] = i;
}
// Let's now create our second thread and ask it to start
// in the silly() function.
_beginthread( silly, 0, (void*)1 );
_beginthread( silly, 0, (void*)2 );
Sleep( 100 );
int a;
cin >> a;
}
void silly( void *arg )
{
printf( "The silly() function was passed %d\n", (INT_PTR)arg ) ;
int partialSum = 0;
for(int i =50*((INT_PTR)arg - 1); i < 50 * ((INT_PTR)arg) ; i++){
partialSum == arraySum[i];
}
}
我似乎觉得很难做的是让函数将分区总和返回给 main 方法。有人可以帮我吗。