| #include <cstdlib> | 
 | #include <iostream> | 
 | #include <mutex> | 
 | #include <thread> | 
 | #include <chrono> | 
 | using namespace std; | 
 | 
 | 
 | std::mutex g_mutex; | 
 | bool g_ready = false; | 
 | int g_data = 0; | 
 | 
 | 
 | int produceData() { | 
 |   int randomNumber = rand() % 1000; | 
 |   std::cout << "produce data: " << randomNumber << "\n"; | 
 |   return randomNumber; | 
 | } | 
 | 
 | 
 | void consumeData(int data) { std::cout << "receive data: " << data << "\n"; } | 
 | 
 | 
 | // consumer thread function | 
 | void consumer() { | 
 |   while (true) { | 
 |     while (!g_ready) { | 
 |       // sleep for 1 second | 
 |       std::this_thread::sleep_for (std::chrono::seconds(1)); | 
 |     } | 
 |     std::unique_lock<std::mutex> ul(g_mutex); | 
 |     consumeData(g_data); | 
 |     g_ready = false; | 
 |   } | 
 | } | 
 | 
 | 
 | // producer thread function | 
 | void producer() { | 
 |   while (true) { | 
 |     std::unique_lock<std::mutex> ul(g_mutex); | 
 | 
 | 
 |     g_data = produceData(); | 
 |     g_ready = true; | 
 |     ul.unlock(); | 
 |     while (g_ready) { | 
 |       // sleep for 1 second | 
 |       std::this_thread::sleep_for (std::chrono::seconds(1)); | 
 |     } | 
 |   } | 
 | } | 
 | 
 | 
 | void consumerThread() { consumer(); } | 
 | 
 | 
 | void producerThread() { producer(); } | 
 | 
 | 
 | int main() { | 
 |   std::thread t1(consumerThread); | 
 |   std::thread t2(producerThread); | 
 |   t1.join(); | 
 |   t2.join(); | 
 |   return 0; | 
 | } | 
1 Comments
// C++ program to demonstrate working of regex_match()
ReplyDelete#include
#include
using namespace std;
int main()
{ cout<<"enter string"<>a;
regex b("(a*abb*)");
if ( regex_match(a, b) )
cout << "String 'a' matches regular expression 'b' \n";
if ( regex_match(a.begin(), a.end(), b) )
{ cout << "String 'a' matches with regular expression " "'b' in the range from 0 to string end\n"; }
else {
cout << "String 'a' DONOT matches with regular expression " "'b' in the range from 0 to string end\n";
}
return 0;
}
If you have any doubts/suggestion/any query or want to improve this article, you can comment down below and let me know. Will reply to you soon.