I want to try THREAD to use it. This is a big feature of Boost. If you add to Standard C , you can do efficiency.
When I debug THREAD at night, I first encountered a problem.
Tell me that Boost_Thread-VC71-MT-GD-1_31.dll can't find it. This problem is like this, because when JAM is only copied to Windows / Systems32 below when JAM, no copy of the DEBUG version of the file. So I hand to copy the files. I thought I thought I had a problem. Let me reappear it.
From the simplest start:
#include
#include
Void Hello ()
{
Std :: cout <<
"Hello World, I'm A Thread!"
<< std :: endl;
}
Int main (int Argc, char * argv [])
{// Creating a Thread This section is created, and immediately returns to the main process, so that the thread is running. // This section and the program design in the MPI multi-machine parallel environment is almost the same. Just one is parallel // one is parallel in a distributed environment.
Boost :: Thread thrd (& hello); thrd.join (); // Waiting for the thread to return
Return 0;
}
Use MUTEX mutual exclusion
#include
#include
#include
Boost :: Mutex IO_MUTEX;
Struct Count
{
COUNT (INT): ID (ID) {}
Void Operator () ()
{
For (int i = 0; i <10; i)
{
Boost :: Mutex :: Scoped_lock
LOCK (IO_MUTEX);
Std :: cout << id << ":"
<< i << std :: endl;
}
}
Int ID;
}
Int main (int Argc, char * argv [])
{
Boost :: Thread thrd1 (count (1));
Boost :: Thread Thrd2 (COUNT (2));
THRD1.JOIN ();
THRD2.JOIN ();
Return 0;
}
More complex, that is, adding a bind to the above code is not to use functor.
#include
#include
#include
#include
Boost :: Mutex IO_MUTEX;
Void Count (INT ID)
{
For (int i = 0; i <10; i)
{
Boost :: Mutex :: Scoped_lock
LOCK (IO_MUTEX);
Std :: cout << ID << ":" <<
i << std :: endl;
}
}
Int main (int Argc, char * argv [])
{
Boost :: Thread thrd1 (
Boost :: Bind (& Count, 1));
Boost :: Thread thrd2 (
Boost :: Bind (& Count, 2)); thrd1.join ();
THRD2.JOIN ();
Return 0;
}
Boost :: Condition
This is more complex
Condition variables are typically used in the combination of Mutex and Shared Resource. A process first locks Mutex and then determines that the shared resource is not available. If he is not then he starts waiting for Conditional Variable. This action makes the thread when Mutex is unlocked. This other thread can change the status of the shared resource. As long as the MUTEX is locked here as long as it is used to use the shared resource. When some processes modify such a shared resource, you need to notify us to wait for the waiting process. This other process can have a WAIT state.
#include
#include
#include
#include
Const int buf_size = 10;
Const int iters = 100;
Boost :: Mutex IO_MUTEX;
Class buffer
{
PUBLIC:
Typedef Boost :: Mutex :: Scoped_lock
Scoped_lock;
Buffer ()
: p (0), c (0), FULL (0)
{
}
Void Put (Int M)
{
Scoped_lock lock;
IF (full == buf_size)
{
{
Boost :: Mutex :: Scoped_lock
LOCK (IO_MUTEX);
Std :: cout <<
"Buffer is full. Waiting ..."
<< std :: endl;
}
While (full == buf_size)
Cond.wait (LOCK);
}
BUF [P] = m;
P = (p 1)% buf_size;
Full;
Cond.Notify_one ();
}
int GET ()
{
Scoped_lock LK (MUTEX);
IF (full == 0)
{
{
Boost :: Mutex :: Scoped_lock
LOCK (IO_MUTEX);
Std :: cout <<
"Buffer is es Empty. Waiting ..."
<< std :: endl;
}
While (fulll == 0)
Cond.wait (LK);
}
INT i = BUF [C];
C = (C 1)% buf_size;
--full;
Cond.Notify_one ();
Return I;
}
Private:
Boost :: mutex mutex;
Boost :: Condition Cond;
Unsigned Int P, C, FULL;
INT buf [buf_size];
}
Buffer buf;
void writer ()
{
For (int N = 0; n { { Boost :: Mutex :: Scoped_lock LOCK (IO_MUTEX); Std :: cout << "Sending:" << n << std :: endl; } BUF.PUT (N); } } Void reader () { For (int x = 0; x INT n = buf.get (); { Boost :: Mutex :: Scoped_lock LOCK (IO_MUTEX); Std :: cout << "received:" << n << std :: endl; } } } Int main (int Argc, char * argv []) { Boost :: Thread Thrd1 (& Reader); Boost :: Thread Thrd2 (& Writer); THRD1.JOIN (); THRD2.JOIN (); Return 0; }