DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2004
    Posts
    2

    Multithreading variables do not update right...

    I am attempting this multithreading exercise and It is supposed to create 4 random numbers in the first thread then send them to Global Variables P_a, P_b, Q_a, Qb and then Thread 2 is supposed to calculate the midpoint, this process repeats until 10 sets of 4 are created, calculated and displayed... For somereason Thread one seems to only want to update my items once.. strangely though it will update them (except for P_a) if i debug in that area however the first figure stays the same... please help... I am a newbie obviously so dont be mad if I am just stupid ;) Thanks. OH I use /MT in the Commandline options.. and I am using .NET 2K3 C++


    #include <process.h>
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    HANDLE hMutex;
    float P_a;
    float P_b;
    float Q_a;
    float Q_b;
    unsigned Counter;
    bool turn;
    bool kill;

    unsigned __stdcall SecondThreadFunc( void* pArguments )
    {
    float Midx;
    float Midy;
    DWORD dwWaitResult;
    //printf( "Enter second thread...\n" );
    while(1) {
    // Request ownership of mutex.
    dwWaitResult = WaitForSingleObject(hMutex, 5000L);
    switch (dwWaitResult){
    // The thread got mutex ownership.
    case WAIT_OBJECT_0:
    __try {
    if (kill) {
    printf( "Kill TRUE -- Thread Two Exiting -- Counter is-> %d\n", Counter );
    _endthreadex( 0 );
    return 0;
    }
    else {
    if (turn == FALSE) {
    ///////////////Calulating////////////////////////////
    Midx = ((P_a + Q_a)/2);
    Midy = ((P_b + Q_b)/2);
    ///////////////////////////////////////////////////
    printf( "MidPoint is -> (%.1f,%.1f)\n", Midx, Midy);
    printf( "Thread Two Counter is-> %d\n", Counter );
    Counter++;
    turn = TRUE;
    }
    }
    }
    __finally {
    // Release ownership of the mutex object.
    if (! ReleaseMutex(hMutex)) {
    printf( "if (! ReleaseMutex(hMutex)) in function\n");
    // Deal with error.
    }
    break;
    }
    }
    }
    _endthreadex( 0 );
    return 0;
    }
    int main()
    {
    HANDLE hThread;
    unsigned threadID;
    DWORD dwWaitResult;
    int i;
    bool endit = FALSE;
    int random_integer;
    int randomNumbers[4];
    turn = TRUE;
    Counter = 0; // initialize Counter
    // Create a mutex with no initial owner.
    hMutex = CreateMutex(NULL,FALSE,"MutexToProtectDatabase");
    if (hMutex == NULL) {
    // Check for error.
    printf( "Error Creating MUTEX -- TERMINATING %d\n", Counter);
    return 0;
    }
    ///printf( "Creating second thread...\n" );
    // Create the second thread.
    hThread = (HANDLE) _beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0,&threadID);
    while (1) {
    //Request ownership of mutex.
    dwWaitResult = WaitForSingleObject(hMutex, 5000L);
    switch (dwWaitResult) {
    // The thread got mutex ownership.
    case WAIT_OBJECT_0:
    __try
    {
    if (turn == TRUE){
    /////////////////RANDOMIZER//////////////////
    /* Seed the random-number generator with current time so that
    * the numbers will be different every time we run.*/
    srand( (unsigned)time( NULL ) );
    /* Display 10 numbers. */
    for( i = 0; i < 4 ;i++ ) {
    random_integer = 1 + int(100 * rand()/(RAND_MAX+1.0));
    randomNumbers[i] = random_integer;
    printf( "Random Numer %3d\n", random_integer );
    }
    /////////////////////////////////////////////
    P_a = randomNumbers[0];
    P_b = randomNumbers[1];
    Q_a = randomNumbers[2];
    Q_b = randomNumbers[3];
    Counter++;
    printf( "Primary Thread Counter is-> %d\n", Counter);
    turn = FALSE;
    if (Counter > 19) {
    kill = TRUE;
    endit = TRUE;
    break;
    }
    }
    }
    __finally
    {
    // Release ownership of the mutex object.
    if (! ReleaseMutex(hMutex)) {
    printf( "if (! ReleaseMutex(hMutex)) in main \n");
    // Deal with error.
    }
    break;
    }
    }
    if (endit) {
    break;
    }
    }
    WaitForSingleObject( hThread, INFINITE);
    printf( "Counter should be 10; it is-> %d\n", Counter );
    // Destroy the HANDLE object.
    CloseHandle( hThread );
    return 0;
    }
    Last edited by carlosrapa; 09-24-2004 at 07:21 AM.

  2. #2
    Join Date
    Sep 2004
    Posts
    2
    dont worry i figured it out i had to initialize the srand up higher in main...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links