Thread: Multithreads for windows

  1. #1
    Registered User
    Join Date
    Mar 2014
    Location
    Zagreb, Croatia, Croatia
    Posts
    15

    Multithreads for windows

    Hi guys,
    I am new to this forum and I've found some interesting stuff so I wanted to ask ya all a question.

    I am fairly new to C/C++, I got general knowledge of most of it's functions but only from general libs (stdio,stdlib,math,string).
    I've also been doing some practice with time.h & windows.h and recently I wanted to try to program a little game.

    It's very simple, it gives a user a random math operation + or - and 2 integer values scaling from 1 to 100.
    All of this is random made with rand() function.
    Now, I wanted to put in a timer which gives a player a time limit to answer but couldn't really find anything usefull apart from the difference in time but what I want is to have the actual timer on screen using a for loop and system("cls").

    Now here is the issue which I guess you all see right away...while my timer is counting I can't make inputs so it's practicly useless.
    I've done some reading on it and ran on a book in the student public library regarding multiprocesses and multithread but right after I got a hang of if from the book I noticed it's only working for UNIX.

    Since then I didn't have much luck finding a solution for windows so if any1 could post some simple code, something like a timer and ability to input text while the timer is counting or a reference to another site.

    Any help would be greatly appriciated.

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Link to an example of a windows multi-threaded dos console program to copy a file, one thread reads, the other writes, using Microsoft windows multithreading objects (mutexes, semaphores), and functions as opposed to generic ones. It uses a custom messaging system to communicate between threads. The code in main() to set this up is a bit tedious, but the actual functions themselves are fairly simple.

    http://rcgldr.net/misc/mtcopy.zip

    For a fixed frequency loop within a thread, reading a high frequency counter along with using Sleep(1) (up to 2ms delay in the case of Win XP) to keep from going cpu bound is useful. Example code:
    Code:
    typedef unsigned long long UI64;        /* unsigned 64 bit int */
    #define FREQ    400                     /* desired frequency rate */
    /* ... */
    LARGE_INTEGER liPerfFreq;               /* 64 bit frequency */
    LARGE_INTEGER liPerfTemp;               /* used for query */
    UI64 uFreq = FREQ;                      /* process frequency */
    UI64 uOrig;                             /* original tick */
    UI64 uWait;                             /* tick rate / freq */
    UI64 uRem = 0;                          /* tick rate % freq */
    UI64 uPrev;                             /* previous tick based on original tick */
    UI64 uDelta;                            /* current tick - previous */
    UI64 u2ms;                              /* 2ms of ticks */
    UI64 i;
    /* ... */
        QueryPerformanceFrequency(&liPerfFreq);
        u2ms = ((UI64)(liPerfFreq.QuadPart)+499) / ((UI64)500);
        timeBeginPeriod(1);                 /* set period to 1ms */
        Sleep(128);                         /* wait for it to stabilize */
        while(1){
            /* ... fixed frequency code goes here ... */
            while(1){        /* delay loop */
                QueryPerformanceCounter(&liPerfTemp);
                uDelta = (UI64)(liPerfTemp.QuadPart - uPrev);
                if(uDelta >= uWait)
                    break;
                if((uWait - uDelta) > u2ms)
                    Sleep(1);
            }
            uPrev += uWait;
            /* ... code to break out of loop goes here ... */
        }
        timeEndPeriod(1);                   /* restore period */
    /* ... */
    Last edited by rcgldr; 03-08-2014 at 12:48 PM.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Am I right in thinking that the new c11 standard includes threads, and using a c11 compliant compiler will allow cross - platform code using threads?
    Last edited by gemera; 03-08-2014 at 04:44 PM.

  4. #4
    Registered User
    Join Date
    Mar 2014
    Location
    Zagreb, Croatia, Croatia
    Posts
    15
    @gemera
    You're right but the functions are different from the ones used for UNIX due to OS restrictions (if I understood it right) so you can't just use stuff like fork() for creating new processes which is sad..., creating multi-threads on windows is kinda tricky if you ask me and I got like 2 examples so far including the one above from rcgldr and it still takes me a while to understand how I use them correctly.

    @rcgldr
    Thanks for the example it helps a lot, got another one somewhere that makes multiple smile-emoticons jumping around the screen. Now all I need to do is study these 2 examples and hopefully I'll get a hang of it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open an excel file from a windows service under Windows Vista
    By AdrianaLuby in forum C# Programming
    Replies: 1
    Last Post: 06-05-2007, 03:55 AM
  2. Completion Port and Multithreads :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 11-06-2002, 11:37 PM
  3. multithreads
    By JagWire in forum C Programming
    Replies: 1
    Last Post: 06-28-2002, 11:22 AM
  4. Multithreads & Pointer to bool :: MFC
    By kuphryn in forum Windows Programming
    Replies: 7
    Last Post: 06-26-2002, 10:50 AM
  5. Replies: 6
    Last Post: 01-07-2002, 02:46 AM