Simple multithreading?

Sorry for a newbie question.

I worte a C++ command line program that monitors a live video stream. The size of the stream is not known, so currently the program executes in an infinite loop.

I can stop it by pressing ctrl+C, but this is nor a very neat solution. I want another thread to run in the background waiting for the user to enter 0, so then it would terminate correctly.

This must be an easy thing to do! Tutorials on multithreading seem long and complicated. Could anyone help please?
Multithreading is a delicate topic. Quite powerful, but you really need to go through several related topics, like synchronization objects. I can only recommend that you do not seek out easy shortcuts and instead read one or two of those long and complex tutorials.
Well there will be no variables accessed from different threads or anything like that... I am looking through the tutorials, but I do hope that for my purpose I don't need to learn anything complicated right now (time is of an issue).
closed account (3hM2Nwbp)
Before I go too deep into it, webJose is right about learning concurrent programming.

boost::thread is the most user friendly threading library that I've had the pleasure of using in C++. While having a much cleaner syntax than Windows / Posix APIs (imo of course), it can be freely used on either platform.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <boost/bind.hpp>
#include <boost/thread.hpp>

class Threadable
{
  public:

    void stop()
    {
        running = false;
    }

    int run()
    {
        running = true;
        while(running)
        {
            //Do Something Meaningful
        }
        return 0;
    }


  private:
    bool running;
};

int main()
{
  Threadable t1, t2;
  boost::thread thread1(boost::bind(&Threadable::run, t1));
  boost::thread thread2(boost::bind(&Threadable::run, t2));

  // Let the threads run for half a second
  boost::this_thread::sleep(boost::posix_time::milliseconds(500));

  // Signal them to stop
  thread1.stop();
  thread2.stop();

  // Wait for them to gracefully exit
  thread1.join();
  thread2.join();
  return 0;
}
looks good, but I already spent half a day trying to install boost libraries and getting different types of errors. Why nothing is ever straight forward?...
Quick and dirty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <windows.h>
#include <process.h>

unsigned _stdcall ThreadFunc(void* params)
{
     //do work

     //end of thread
     _endthreadex(0);
     return 0;
}

int main()
{
     unsigned threadID;
     HANDLE hThread;

     //start the thread (calling without parameters)
     hThread = (HANDLE)_beginthreadex( NULL, 0, &ThreadFunc, NULL, 0, &threadID );

     //do main thread work

     //destroy the thread object (make sure the thread is done at this point)
     CloseHandle( hThread );

     return 0;
}
FYI, you do not need to call _endthreadex() yourself. See http://msdn.microsoft.com/en-us/library/kdzttdcb(VS.80).aspx .
thanks shacktar, this is what I needed! :)
Topic archived. No new replies allowed.