Ace condition class

Source: Internet
Author: User

When I came in, I encountered some questions about the ace code. Today I saw an article about the ace condition category on the Internet, which solved some of my doubts. In view of the idea of spreading many good things, I will try again. Original in
Http://www.cnblogs.com/TianFang/archive/2006/12/04/581854.html
Ace condition category

Ace condition class (condition variable) provides different locking mechanisms for styles and mutex, Reader/author locks, and counting semaphores. When the lock-holding thread executes code in the critical section, these three mechanisms let the collaboration thread wait. On the contrary, condition variables are usually used by a thread to wait until a condition expression involving shared data reaches a specific State. When another collaboration thread indicates that the status of the shared data has changed, the scheduler wakes up a thread suspended on the condition variable. Therefore, the newly awakened thread recalculates its conditional expression. If the shared data has reached the appropriate state, it will resume processing.

The ACE thread Library provides a class called condition to implement conditional variable semantics in the C ++ packaging class. The definition is as follows:
Ace_thread_mutex mutex;
Ace_condition <ace_thread_mutex> cond (mutex );

This object has two common methods.

Signal ()
Sends a signal that meets the condition to other threads that use the condition variable.
Wait ()
Check whether the conditions are met. If the conditions are met, continue to execute. If the conditions are not met, the main thread will wait for this condition variable. The condition variable Automatically releases the mutex and enables the main thread to sleep.
Conditional variables are always used with mutex. This is a general mode that can be described as follows:

While (expression not true) wait on condition variable;

Condition variables are not used for mutex and are often used for inter-thread collaboration. The following example demonstrates how to implement thread collaboration through condition variables.

# Include "ACE/thread. H"
# Include "ACE/synch. H"

# Include <iostream>
Using namespace STD;

Ace_thread_mutex mutex;
Ace_condition <ace_thread_mutex> cond (mutex );

Void * worker (void * Arg)
{
Ace_ OS: Sleep (2); // ensure that the cond. Wait () of the eater thread is executed first in the cond. Signal () of the worker thread
Mutex. Acquire ();
Ace_ OS: Sleep (1 );
Cout <Endl <"produce" <Endl;
Cond. Signal ();
Mutex. Release ();
Return NULL;
}

Void * eater (void * Arg)
{
Mutex. Acquire ();
Cond. Wait ();
Cout <Endl <"eat" <Endl;
Mutex. Release ();
Return NULL;
}

Int main (INT argc, char * argv [])
{
Ace_thread: spawn (ace_thr_func) Worker );
Ace_ OS: Sleep (1 );
Ace_thread: spawn (ace_thr_func) eater );

While (true)
Ace_ OS: Sleep (10 );

Return 0;
}

In this example, a producer thread worker and a consumer thread eater are created first. The consumer thread executes faster than the producer thread. If two threads do not limit the concurrent execution, the consumption will occur first, post-production situation (only the mutex lock cannot be well solved, so it is impossible for the producer to obtain the mutex first ). Therefore, we use the conditional variable notification method to ensure the sequential execution of threads:

The consumer thread obtains the mutex and waits for the condition to be met (the producer has produced the food ). Release the mutex at the same time and enter the sleep state.
The producer obtains the mutex (although the consumer obtains the mutex first, the wait function called by the consumer releases the consumer's mutex, the Conditional Variable sends a signal (calls the signal function) to notify the consumer of the completion of production, end the production process, and release the mutex.
After receiving the signal, the consumer obtains the mutex again to complete the consumption process.

Notes for using conditional variables:

Conditional variables must be used with mutex, that is, they must be locked before use (call the mutex acquire function). After use, the mutex must be released.
If wait () and signal () in the condition variable are used in pairs, the wait () function must be executed before signal () to ensure that wait () can receive a notification that the condition is met, do not wait until a deadlock is formed (the first sentence in the worker thread is this role ).

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.