Skip to content

Commit

Permalink
Channel structure has been completed
Browse files Browse the repository at this point in the history
  • Loading branch information
leventkaragol committed May 21, 2024
1 parent 3d840c4 commit 8be244c
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 7 deletions.
10 changes: 8 additions & 2 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ cmake_minimum_required(VERSION 3.14)

project(examples)

add_executable(examples single-producer-single-consumer.cpp)
add_executable(single-producer-single-consumer single-producer-single-consumer.cpp)
add_executable(single-producer-multiple-consumer single-producer-multiple-consumer.cpp)
add_executable(multiple-producer-single-consumer multiple-producer-single-consumer.cpp)
add_executable(multiple-producer-multiple-consumer multiple-producer-multiple-consumer.cpp)

target_link_libraries(examples PRIVATE libcpp-channel)
target_link_libraries(single-producer-single-consumer PRIVATE libcpp-channel)
target_link_libraries(single-producer-multiple-consumer PRIVATE libcpp-channel)
target_link_libraries(multiple-producer-single-consumer PRIVATE libcpp-channel)
target_link_libraries(multiple-producer-multiple-consumer PRIVATE libcpp-channel)
53 changes: 53 additions & 0 deletions examples/multiple-producer-multiple-consumer.cpp
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;
}
50 changes: 50 additions & 0 deletions examples/multiple-producer-single-consumer.cpp
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;
}
50 changes: 50 additions & 0 deletions examples/single-producer-multiple-consumer.cpp
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;
}
10 changes: 5 additions & 5 deletions examples/single-producer-single-consumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

using namespace lklibs;

void producer(Channel<std::string>::Producer producer)
void produce(Channel<std::string>::Producer producer)
{
auto i = 0;

Expand All @@ -15,11 +15,11 @@ void producer(Channel<std::string>::Producer producer)
// Sending string message to the consumer
producer.send("Message " + std::to_string(i));

std::this_thread::sleep_for(std::chrono::milliseconds(500));
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}

void consumer(Channel<std::string>::Consumer consumer)
void consume(Channel<std::string>::Consumer consumer)
{
while (true)
{
Expand All @@ -43,10 +43,10 @@ int main()
auto consumer = channel.getConsumer();

// Passing producer object to the first thread
std::thread producer_thread(::producer, std::move(producer));
std::thread producer_thread(::produce, std::move(producer));

// Passing consumer object to the second thread
std::thread consumer_thread(::consumer, std::move(consumer));
std::thread consumer_thread(::consume, std::move(consumer));

producer_thread.join();
consumer_thread.join();
Expand Down

0 comments on commit 8be244c

Please sign in to comment.