I'd like (eventually) to implement parallel search.
I've been spending some time looking at threads, and have made a very simple program (basically like on the MSDN site) to create threads and initialise them...
Code: Select all
#define MAX_THREADS 3
#define BUF_SIZE 255
using namespace std;
DWORD WINAPI Thread_Init( LPVOID input );
typedef struct sMyPosition {
int alpha;
int beta;
int board[64];
} MYPOS, *PMYPOS;
MYPOS PosArray[MAX_THREADS];
DWORD dwThreadIdArray[MAX_THREADS];
HANDLE hThreadArray[MAX_THREADS];
int main()
{
// Create MAX_THREADS worker threads.
for( int i=0; i<MAX_THREADS; i++ )
{
// Generate unique data for each thread to work with.
PosArray[i].alpha = i-10000;
PosArray[i].beta = i+10000;
// Create the thread to begin execution on its own.
hThreadArray[i] = CreateThread(
NULL, // default security attributes
0, // use default stack size
Thread_Init, // thread function name
&PosArray[i], // argument to thread function
0, // use default creation flags
&dwThreadIdArray[i]); // returns the thread identifier
// Check the return value for success.
// If CreateThread fails, terminate execution.
// This will automatically clean up threads and memory.
if (hThreadArray[i] == NULL)
{
ExitProcess(3);
}
} // End of main thread creation loop.
// Wait until all threads have terminated.
WaitForMultipleObjects(MAX_THREADS, hThreadArray, TRUE, INFINITE);
for(int i=0; i<MAX_THREADS; i++)
{
CloseHandle(hThreadArray[i]);
}
return 0;
}
DWORD WINAPI Thread_Init( LPVOID input )
{
PMYPOS pData;
pData = (PMYPOS)input;
printf("\n Creating... ");
printf("Parameters = %d, %d\n",pData->alpha, pData->beta);
return 0;
}
What I'm really struggling with is the concept of setting the threads "doing" something in parallel.
What I had in mind was a simple function that prints output at random intervals from each thread, so I can see the evidence of the parallel computing.
But how do I do this? I couldn't find much on MSDN.... it's hard enough to undertand as it is

Thanks for any replies!!!