I know I am going to have alot of questions but can someone check to see if I am starting this out correctly...
Programming assignment two will demonstrate your understanding and mastery of multi-threaded application concepts in a Windows environment using Visual C++ .NET 2005. In this programming assignment you will create a multithreaded application that computes the greatest common divisor using Euclid’s algorithm. Your program is to create multiple threads. Thread one must pick two random numbers between 1 and 100, display the value of the random numbers selected (along with a thread identifier), and place them into shared global variables. Thread one then must wait for thread two to act on the values. Once thread two has completed operating on the variables, thread one must repeat, then thread two, and so on, until ten sets of random numbers have been picked, greatest common divisor found, displayed, and placed in the shared global variables. The two random numbers picked by thread one become the two input values for Euclid’s algorithm. Thread two must act in a cooperative manner with thread one. Thread two’s task is to find the greatest common divisor for the two random numbers selected. Once the greatest common divisor is found, it should be displayed (along with a thread identifier). Thread two should then wait for another set of values to be placed in the shared global variables. Thread two should act upon each set of random numbers selected by thread one. Your program must use two threads You may choose managed or unmanaged code for this programming assignment. Your program may use any of the available libraries (such as the ATL or the STL). The random number generator should be seeded in some manner. During program development and testing, using a static number to seed the random number generator will result in the same numbers being picked each time the program is run. For production, seeding with the current time is preferred. Using the current time insures that different numbers will be generated each time the program is run. Your program must use some method of synchronization for the cooperating threads. You may use any synchronization object (semaphore, mutex, ATL object, etc) that is available in Visual C++ .NET. Your program must not use busy-waiting. Your program’s threads must not access the shared global variables in any manner unless they do so in a way that assures that the other thread has been blocked from accessing the variables and that any operation can occur in a mutually exclusive manner. Failure to access the shared global variables in a mutually exclusive manner will be considered a major design flaw. Turn in a printed copy of your program source as well as including the source on floppy disk (no executable files) or email the program source, by pasting it into the email (no attachments), to the instructor. Insure your name is on all materials. Programs will be graded based upon program correctness, good programming techniques, style, and user interface. Ten points of extra credit will be awarded to programs that correctly implement mutual exclusion using a binary semaphore. This is alll I have found so far and do not think it is right..
#include <process.h>
#include <windows.h>
#include <stdio.h>
unsigned Counter;
unsigned __stdcall SecondThreadFunc( void* pArguments ) {
printf( "In second thread...\n" );
while ( Counter < 1000000 )
Counter++;
_endthreadex( 0 );
return 0;
}
void main()
{
HANDLE hThread;
unsigned threadID;
printf( "Creating second thread...\n" );
// Create the second thread.
hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0,
&threadID );
// Wait until second thread has terminated. If you comment out the line
// below, Counter will not be correct because the thread has not
// terminated, and Counter most likely has not been incremented to
// 1000000 yet.
WaitForSingleObject( hThread, INFINITE );
printf( "Counter should be 1000000; it is-> %d\n", Counter );
// Destroy the thread object.
CloseHandle( hThread );
}