-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Channel structure has been completed
- Loading branch information
1 parent
3d840c4
commit 8be244c
Showing
5 changed files
with
166 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,61 @@ | ||
#include "libcpp-channel.hpp" | ||
#include <iostream> | ||
#include <thread> | ||
|
||
using namespace lklibs; | ||
|
||
void produce(Channel<std::string>::Producer producer, const std::string& name) | ||
{ | ||
auto i = 0; | ||
|
||
while (true) | ||
{ | ||
i++; | ||
|
||
// Sending string message to the consumer with producer name | ||
producer.send(name + "-Message " + std::to_string(i)); | ||
|
||
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); | ||
} | ||
} | ||
|
||
void consume(Channel<std::string>::Consumer consumer, const std::string& name) | ||
{ | ||
while (true) | ||
{ | ||
// Receiving message from the producers | ||
auto message = consumer.receive(); | ||
|
||
if (message.has_value()) | ||
{ | ||
std::cout << name <<"-Received: " << message.value() << std::endl; | ||
} | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
// Creating a string channel | ||
Channel<std::string> channel; | ||
|
||
// Getting producer and consumer objects | ||
auto producer1 = channel.getProducer(); | ||
auto producer2 = channel.getProducer(); | ||
auto consumer1 = channel.getConsumer(); | ||
auto consumer2 = channel.getConsumer(); | ||
|
||
// Passing producer objects to producer threads | ||
std::thread producer1_thread(::produce, std::move(producer1), "Producer1"); | ||
std::thread producer2_thread(::produce, std::move(producer2), "Producer2"); | ||
|
||
// Passing consumer object to the second thread | ||
std::thread consumer1_thread(::consume, std::move(consumer1), "Consumer1"); | ||
std::thread consumer2_thread(::consume, std::move(consumer2), "Consumer2"); | ||
|
||
producer1_thread.join(); | ||
producer2_thread.join(); | ||
consumer1_thread.join(); | ||
consumer2_thread.join(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,58 @@ | ||
#include "libcpp-channel.hpp" | ||
#include <iostream> | ||
#include <thread> | ||
|
||
using namespace lklibs; | ||
|
||
void produce(Channel<std::string>::Producer producer, const std::string& name) | ||
{ | ||
auto i = 0; | ||
|
||
while (true) | ||
{ | ||
i++; | ||
|
||
// Sending string message to the consumer with producer name | ||
producer.send(name + "-Message " + std::to_string(i)); | ||
|
||
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); | ||
} | ||
} | ||
|
||
void consume(Channel<std::string>::Consumer consumer) | ||
{ | ||
while (true) | ||
{ | ||
// Receiving message from the producers | ||
auto message = consumer.receive(); | ||
|
||
if (message.has_value()) | ||
{ | ||
std::cout << "Received: " << message.value() << std::endl; | ||
} | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
// Creating a string channel | ||
Channel<std::string> channel; | ||
|
||
// Getting producer and consumer objects | ||
auto producer1 = channel.getProducer(); | ||
auto producer2 = channel.getProducer(); | ||
auto consumer = channel.getConsumer(); | ||
|
||
// Passing producer objects to producer threads | ||
std::thread producer1_thread(::produce, std::move(producer1), "Producer1"); | ||
std::thread producer2_thread(::produce, std::move(producer2), "Producer2"); | ||
|
||
// Passing consumer object to the second thread | ||
std::thread consumer_thread(::consume, std::move(consumer)); | ||
|
||
producer1_thread.join(); | ||
producer2_thread.join(); | ||
consumer_thread.join(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,58 @@ | ||
#include "libcpp-channel.hpp" | ||
#include <iostream> | ||
#include <thread> | ||
|
||
using namespace lklibs; | ||
|
||
void produce(Channel<std::string>::Producer producer) | ||
{ | ||
auto i = 0; | ||
|
||
while (true) | ||
{ | ||
i++; | ||
|
||
// Sending string message to the consumer | ||
producer.send("Message " + std::to_string(i)); | ||
|
||
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); | ||
} | ||
} | ||
|
||
void consume(Channel<std::string>::Consumer consumer, const std::string& name) | ||
{ | ||
while (true) | ||
{ | ||
// Receiving message from the producer | ||
auto message = consumer.receive(); | ||
|
||
if (message.has_value()) | ||
{ | ||
std::cout << name << "-Received: " << message.value() << std::endl; | ||
} | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
// Creating a string channel | ||
Channel<std::string> channel; | ||
|
||
// Getting producer and consumer objects | ||
auto producer = channel.getProducer(); | ||
auto consumer1 = channel.getConsumer(); | ||
auto consumer2 = channel.getConsumer(); | ||
|
||
// Passing producer object to the first thread | ||
std::thread producer_thread(::produce, std::move(producer)); | ||
|
||
// Passing consumer object to the second thread | ||
std::thread consumer1_thread(::consume, std::move(consumer1), "Consumer1"); | ||
std::thread consumer2_thread(::consume, std::move(consumer2), "Consumer2"); | ||
|
||
producer_thread.join(); | ||
consumer1_thread.join(); | ||
consumer2_thread.join(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters