Need help with C++ Multithreading/Pointers

Discussion in 'Programmer's Corner' started by Brandon, Feb 27, 2005.

  1. Brandon

    Thread Starter Senior Member

    Dec 14, 2004
    306
    0
    For some reason I can't find a site or through VC++ help, an easy explination of how to impliment multi threading in C++.

    I understand the concepts of it, mutexes, multithread debugging and concurrency, but every time I find about how to do a multithreaded program, it all looks like greek.

    I don't need an indepth understanding of it, I just need to do a basic 4 threaded program. 3 of the threads need a parameter passed to it. Its going to be a class. The last thread is to watch keyboard input while the program is running.

    Also, pointers. Why Pointers? Read about them, used them, can't really find a why and when you SHOULD use them.

    Thank you for any help.
     
  2. Brandon

    Thread Starter Senior Member

    Dec 14, 2004
    306
    0
    If you want to do some EASY multi-threading in C++, go pick up MUD Game Programming by Ron Penton. He has generated a simple multi threading class and mutex arrangement that is cake which. He used it to make saves n what not without laggin down the system, but you can apply them just about anywhere.

    I used 3 threads to do a var update routine and screen display while awaiting a key to be pressed to pause the program.

    SImple as

    ThreadLib::ThreadID A,B,C etc;
    ThreadLib::Mutex m1,m2, etc

    A=ThreadLib::Create(FuncName1,(void*) YourONEvarToPass);
    //Thread execution would start right then in what ever function your writing.and then continue on in the current function while the thread begins to run in parallel.

    void FuncName1(void* YourVar)
    {
    m1.lock(); //Mutex locks. Damn this is simple
    func body;
    m1.unlock(); //who would have guessed?
    return;
    }

    the void* for those that don't know is an untyped varible. You can send ANYTHING into it, but only 1 varible may pass. You just got to type cast it once within the thread. I.e. int YourVar, or YourClass YourVar, etc.
     
Loading...