Skip to main content
Tweeted twitter.com/StackCodeReview/status/1261944562601340928
added 333 characters in body
Source Link

I designed a simple timeout thrower for a bluetooth protocol I am writing. If the packet is not received in a certain amount of time, then timeout is thrown. I made it to be as plug and play as possible.

timeout.hpp:

#ifndef __timeout_h__
#define __timeout_h__

#include <thread>

struct Timeoutthread{
    private:
        int sleep;
    public:
        Timeoutthread(int seconds);
        void time(bool* alert);
};

class Timeout{
    private:
        int sleep;
        bool alert;
    public:
        Timeout(int seconds);
        bool timeout();
};

#endif

timeout.cpp:

#include "timeout.hpp"

Timeout::Timeout(int seconds){
    sleep = seconds;
    Timeoutthread timeoutthread(sleep);
    std::thread timeout(&Timeoutthread::time,timeoutthread,&alert);
    timeout.detach();
}

bool Timeout::timeout(){
    return alert;
}

Timeoutthread::Timeoutthread(int seconds){
    sleep = seconds;
}

void Timeoutthread::time(bool* alert){
    std::this_thread::sleep_for (std::chrono::seconds(sleep));
    *alert = true;
}

simple implementation:

#include <iostream>
#include "timeout.hpp"

int main(){
     std::string message = "";
     Timeout timeout(10);
     while(message == ""){
          message = readBLE(read from bluetooth);
          if(timeout.timeout() == true)
               message = "timed out";
     }

     std::cout<<message<<std::endl;
          
     return 0;
}

The loop waits for a bluetooth message, but if no message is received in 10 seconds, then a timeout is thrown.

Is this an efficient way to do it. I want to make sure that I am not wasting any resources.

EDIT:

There seems to be some confusion on the readBLE() part. There is a lot more code associated with this part but it does not act like std::cin where it is waiting for a user input. Whenever readBLE() is called, if there was no message received, then it would just return an empty string. Sorry for the confusion.

I designed a simple timeout thrower for a bluetooth protocol I am writing. If the packet is not received in a certain amount of time, then timeout is thrown. I made it to be as plug and play as possible.

timeout.hpp:

#ifndef __timeout_h__
#define __timeout_h__

#include <thread>

struct Timeoutthread{
    private:
        int sleep;
    public:
        Timeoutthread(int seconds);
        void time(bool* alert);
};

class Timeout{
    private:
        int sleep;
        bool alert;
    public:
        Timeout(int seconds);
        bool timeout();
};

#endif

timeout.cpp:

#include "timeout.hpp"

Timeout::Timeout(int seconds){
    sleep = seconds;
    Timeoutthread timeoutthread(sleep);
    std::thread timeout(&Timeoutthread::time,timeoutthread,&alert);
    timeout.detach();
}

bool Timeout::timeout(){
    return alert;
}

Timeoutthread::Timeoutthread(int seconds){
    sleep = seconds;
}

void Timeoutthread::time(bool* alert){
    std::this_thread::sleep_for (std::chrono::seconds(sleep));
    *alert = true;
}

simple implementation:

#include <iostream>
#include "timeout.hpp"

int main(){
     std::string message = "";
     Timeout timeout(10);
     while(message == ""){
          message = (read from bluetooth);
          if(timeout.timeout() == true)
               message = "timed out";
     }

     std::cout<<message<<std::endl;
          
     return 0;
}

The loop waits for a bluetooth message, but if no message is received in 10 seconds, then a timeout is thrown.

Is this an efficient way to do it. I want to make sure that I am not wasting any resources.

I designed a simple timeout thrower for a bluetooth protocol I am writing. If the packet is not received in a certain amount of time, then timeout is thrown. I made it to be as plug and play as possible.

timeout.hpp:

#ifndef __timeout_h__
#define __timeout_h__

#include <thread>

struct Timeoutthread{
    private:
        int sleep;
    public:
        Timeoutthread(int seconds);
        void time(bool* alert);
};

class Timeout{
    private:
        int sleep;
        bool alert;
    public:
        Timeout(int seconds);
        bool timeout();
};

#endif

timeout.cpp:

#include "timeout.hpp"

Timeout::Timeout(int seconds){
    sleep = seconds;
    Timeoutthread timeoutthread(sleep);
    std::thread timeout(&Timeoutthread::time,timeoutthread,&alert);
    timeout.detach();
}

bool Timeout::timeout(){
    return alert;
}

Timeoutthread::Timeoutthread(int seconds){
    sleep = seconds;
}

void Timeoutthread::time(bool* alert){
    std::this_thread::sleep_for (std::chrono::seconds(sleep));
    *alert = true;
}

simple implementation:

#include <iostream>
#include "timeout.hpp"

int main(){
     std::string message = "";
     Timeout timeout(10);
     while(message == ""){
          message = readBLE();
          if(timeout.timeout() == true)
               message = "timed out";
     }

     std::cout<<message<<std::endl;
          
     return 0;
}

The loop waits for a bluetooth message, but if no message is received in 10 seconds, then a timeout is thrown.

Is this an efficient way to do it. I want to make sure that I am not wasting any resources.

EDIT:

There seems to be some confusion on the readBLE() part. There is a lot more code associated with this part but it does not act like std::cin where it is waiting for a user input. Whenever readBLE() is called, if there was no message received, then it would just return an empty string. Sorry for the confusion.

added 4 characters in body
Source Link

I designed a simple timeout thrower for a bluetooth protocol I am writing. If the packet is not received in a certain amount of time, then timeout is thrown. I made it to be as plug and play as possible.

timeout.hpp:

#ifndef __timeout_h__
#define __timeout_h__

#include <thread>

struct Timeoutthread{
    private:
        int sleep;
    public:
        Timeoutthread(int seconds);
        void time(bool* alert);
};

class Timeout{
    private:
        int sleep;
        bool alert;
    public:
        Timeout(int seconds);
        bool timeout();
};

#endif

timeout.cpp:

#include "timeout.hpp"

Timeout::Timeout(int seconds){
    sleep = seconds;
    Timeoutthread timeoutthread(sleep);
    std::thread timeout(&Timeoutthread::time,timeoutthread,&alert);
    timeout.detach();
}

bool Timeout::timeout(){
    return alert;
}

Timeoutthread::Timeoutthread(int seconds){
    sleep = seconds;
}

void Timeoutthread::time(bool* alert){
    std::this_thread::sleep_for (std::chrono::seconds(sleep));
    *alert = true;
}

simple implementation:

#include <iostream>
#include "timeout.hpp"

int main(){
     std::string message = "";
     Timeout timeout(10);
     while(message == ""){
          message = (read from bluetooth);
          if(timeout.timeout() == true)
               message = "timed out";
     }

     std::cout<<message<<std::endl;
          
     return 0;
}

The loop waits for a bluetooth message, but if itno message is not received in 10 seconds, then a timeout is thrown.

Is this an efficient way to do it. I want to make sure that I am not wasting any resources.

I designed a simple timeout thrower for a bluetooth protocol I am writing. If the packet is not received in a certain amount of time, then timeout is thrown. I made it to be as plug and play as possible.

timeout.hpp:

#ifndef __timeout_h__
#define __timeout_h__

#include <thread>

struct Timeoutthread{
    private:
        int sleep;
    public:
        Timeoutthread(int seconds);
        void time(bool* alert);
};

class Timeout{
    private:
        int sleep;
        bool alert;
    public:
        Timeout(int seconds);
        bool timeout();
};

#endif

timeout.cpp:

#include "timeout.hpp"

Timeout::Timeout(int seconds){
    sleep = seconds;
    Timeoutthread timeoutthread(sleep);
    std::thread timeout(&Timeoutthread::time,timeoutthread,&alert);
    timeout.detach();
}

bool Timeout::timeout(){
    return alert;
}

Timeoutthread::Timeoutthread(int seconds){
    sleep = seconds;
}

void Timeoutthread::time(bool* alert){
    std::this_thread::sleep_for (std::chrono::seconds(sleep));
    *alert = true;
}

simple implementation:

#include <iostream>
#include "timeout.hpp"

int main(){
     std::string message = "";
     Timeout timeout(10);
     while(message == ""){
          message = (read from bluetooth);
          if(timeout.timeout() == true)
               message = "timed out";
     }

     std::cout<<message<<std::endl;
          
     return 0;
}

The loop waits for a bluetooth message, but if it is not received in 10 seconds, then a timeout is thrown.

Is this an efficient way to do it. I want to make sure that I am not wasting any resources.

I designed a simple timeout thrower for a bluetooth protocol I am writing. If the packet is not received in a certain amount of time, then timeout is thrown. I made it to be as plug and play as possible.

timeout.hpp:

#ifndef __timeout_h__
#define __timeout_h__

#include <thread>

struct Timeoutthread{
    private:
        int sleep;
    public:
        Timeoutthread(int seconds);
        void time(bool* alert);
};

class Timeout{
    private:
        int sleep;
        bool alert;
    public:
        Timeout(int seconds);
        bool timeout();
};

#endif

timeout.cpp:

#include "timeout.hpp"

Timeout::Timeout(int seconds){
    sleep = seconds;
    Timeoutthread timeoutthread(sleep);
    std::thread timeout(&Timeoutthread::time,timeoutthread,&alert);
    timeout.detach();
}

bool Timeout::timeout(){
    return alert;
}

Timeoutthread::Timeoutthread(int seconds){
    sleep = seconds;
}

void Timeoutthread::time(bool* alert){
    std::this_thread::sleep_for (std::chrono::seconds(sleep));
    *alert = true;
}

simple implementation:

#include <iostream>
#include "timeout.hpp"

int main(){
     std::string message = "";
     Timeout timeout(10);
     while(message == ""){
          message = (read from bluetooth);
          if(timeout.timeout() == true)
               message = "timed out";
     }

     std::cout<<message<<std::endl;
          
     return 0;
}

The loop waits for a bluetooth message, but if no message is received in 10 seconds, then a timeout is thrown.

Is this an efficient way to do it. I want to make sure that I am not wasting any resources.

Source Link

Timeout thrower using multithreading

I designed a simple timeout thrower for a bluetooth protocol I am writing. If the packet is not received in a certain amount of time, then timeout is thrown. I made it to be as plug and play as possible.

timeout.hpp:

#ifndef __timeout_h__
#define __timeout_h__

#include <thread>

struct Timeoutthread{
    private:
        int sleep;
    public:
        Timeoutthread(int seconds);
        void time(bool* alert);
};

class Timeout{
    private:
        int sleep;
        bool alert;
    public:
        Timeout(int seconds);
        bool timeout();
};

#endif

timeout.cpp:

#include "timeout.hpp"

Timeout::Timeout(int seconds){
    sleep = seconds;
    Timeoutthread timeoutthread(sleep);
    std::thread timeout(&Timeoutthread::time,timeoutthread,&alert);
    timeout.detach();
}

bool Timeout::timeout(){
    return alert;
}

Timeoutthread::Timeoutthread(int seconds){
    sleep = seconds;
}

void Timeoutthread::time(bool* alert){
    std::this_thread::sleep_for (std::chrono::seconds(sleep));
    *alert = true;
}

simple implementation:

#include <iostream>
#include "timeout.hpp"

int main(){
     std::string message = "";
     Timeout timeout(10);
     while(message == ""){
          message = (read from bluetooth);
          if(timeout.timeout() == true)
               message = "timed out";
     }

     std::cout<<message<<std::endl;
          
     return 0;
}

The loop waits for a bluetooth message, but if it is not received in 10 seconds, then a timeout is thrown.

Is this an efficient way to do it. I want to make sure that I am not wasting any resources.