Boost Thread Details

Source: Internet
Author: User

Break point for thread

Thread::join Mythread::join calls the thread of this method into the wait state until MyThread represents the thread that completes the
Thread::try_join_for
Thread::try_join_until
Blocking waits for a certain period of time
Condition_variable_any::wait Wait (MU)
Condition_variable_any::wait_for
Condition_variable_any::wait_until
Condition_variable_any::notify_one
Condition_variable_any::notify_all
Notifies one/all waiting threads
This_thread::sleep_for
This_thread::sleep_until
The current thread discards the time slice, specifies the blocking time, allows other threads of the same priority to run, and does not release resources
This_thread::yield The current thread discards the time slice, does not specify a blocking time, allows other threads of the same priority to run, and does not release resources
Thread::d Etach Detach thread
Thread::interrupt Break Thread in

Mutual exclusion Lock Boost::mutex mu
Boost::mutex::scoped_lock Lock (MU)
Read/write Lock Boost::shared_mutex Rw_mu
boost::shared_lock<boost::shared_mutex> Lock (RW_MU)
Condition Amount Boost::mutex Iomu
Boost::condition_variable_any Condput
Boost::mutex::scoped_lock Lock (Iomu)
Condput.wait (Iomu)
Condput.notify_one ()

Mutual exclusion Lock:

int g_num = 0;
Boost::mutex Mu; Defining a Mutex Object

int Func (int ncount)
{
for (int i = 0; i < ncount; i++)
{
Boost::mutex::scoped_lock Lock (MU); Operation of shared data requires lock-in
g_num++;
cout << __function__ << ":" << g_num << Endl;
cout << boost::this_thread::get_id () << ":" << g_num << Endl;
}
return g_num;
}

int _tmain (int argc, _tchar* argv[])
{
Boost::thread Th1 (Func, 100);
Boost::thread Th2 (Func, 200);
Th1.join ();
Th2.join ();

cout << boost::this_thread::get_id () << ":" << "Main_end" << Endl;

Th1 and Th2 execution sequence, but must be one after the execution of another will be executed, the last is Main_end

System ("pause");
return 0;
}

Read/write Lock:

int g_num = 0;
Boost::shared_mutex Rw_mu; Defining read-Write Locks

int Write (int ncount)
{
for (int i = 0; i < ncount; i++)
{
boost::unique_lock<boost::shared_mutex> Lock (RW_MU); Add unique Lock
g_num++;
cout << __function__ << ":" << g_num << Endl;
cout << boost::this_thread::get_id () << ":" << __function__ << ":" << g_num << Endl;
}
return g_num;
}

void Read (int ncount)
{
for (int i = 0; i < ncount; i++)
{
boost::shared_lock<boost::shared_mutex> Lock (RW_MU); Plus shared lock
cout << __function__ << ":" << g_num << Endl;
cout << boost::this_thread::get_id () << ":" << __function__ << ": (" << i << "):" < ;< g_num << Endl;
}
}

int _tmain (int argc, _tchar* argv[])
{
Boost::thread Th1 (Write, 100);
Boost::thread Th2 (Read, 100);
Boost::thread Th3 (Read, 100);

Th1.join ();
Th2.join ();
Th3.join ();

System ("pause");

return 0;
}

Condition Amount:

Boost::mutex G_iomutex; Output Control Lock

Template<typename t>
Class Cmsgqueue
{
Public
Cmsgqueue (size_t N): m_ncapacity (N)
{
}

void Push (const t& val)
{
{
Boost::mutex::scoped_lock Lock (M_MU); Locking
while (m_val.size () = = m_ncapacity)//queue is full
{
{
Boost::mutex::scoped_lock Lock (G_iomutex);
cout << "Queue full" << Endl;
}
M_condpush.wait (M_MU); Wait, will be temporarily unlocked
}
M_val.push (Val); Adding data to a queue
}
M_condpop.notify_one (); Notify Read thread
}

void Pop (t& val)
{
{
Boost::mutex::scoped_lock Lock (M_MU); Locking
while (m_val.size () = = 0)//queue is empty
{
{
Boost::mutex::scoped_lock Lock (G_iomutex);
cout << "queue is empty" << Endl;
}
M_condpop.wait (M_MU); Waiting to be readable,
}
val = M_val.front (); Reading data
M_val.pop ();
}
M_condpush.notify_one (); Notifies the write thread
}

Private
Queue<t> M_val; Queue
int m_ncapacity; Maximum queue capacity
Boost::condition_variable_any M_condpush; Write Condition Amount
Boost::condition_variable_any M_condpop; Read Condition Amount
Boost::mutex M_mu; Mutual exclusion Lock
};

Cmsgqueue<int> G_numqueue (10);

void Push (int ncount)
{
    for (int i = 0; i < ncount; i++)
    {
 & nbsp;      {
            Boost::mutex::scoped_lock Lock (G_iomutex);
            cout << boost::this_thread::get_id () << ":" << __function__ << ": Before_push:" << i << Endl;
       }   
         G_numqueue.push (i);
   }
}

void Pop (int ncount)
{
for (int i = 0; i < ncount; i++)
{
int Val;
G_numqueue.pop (Val);
Boost::mutex::scoped_lock Lock (G_iomutex);
cout << boost::this_thread::get_id () << ":" << __function__ << ": After_pop (" << i << "):" << val << Endl;
}
}

int _tmain (int argc, _tchar* argv[])
{
Boost::thread Th1 (Push, 50);
Boost::thread Th2 (Pop, 20);
Boost::thread Th3 (Pop, 30);

Th1.join ();
Th2.join ();
Th3.join ();

System ("pause");

return 0;
}

Not to be continued ...

Boost Thread Details

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.