Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8092411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:16:41+00:00 2026-06-05T20:16:41+00:00

I have a requirement to embed a thread inside a C++ class, kind of

  • 0

I have a requirement to embed a thread inside a C++ class, kind of active object but not exactly. i am spawning thread from the constructor of the class , is it ok to do this way are there any problems with this approach.

#include <iostream>
#include <thread>
#include <unistd.h>

class xfer
{
        int i;
        std::shared_ptr<std::thread> thr;
        struct runnable {
                friend class xfer;
                void operator()(xfer *x) {
                        std::cerr<<"thread started, xfer.i:"<<x->i;
                }
        } run;
        public:
        xfer() try : i(100), thr(new std::thread(std::bind(run, this))) { } catch(...) { }
        ~xfer() { thr->join(); }
};

int
main(int ac, char **av)
{
        xfer x1;
        return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:16:42+00:00Added an answer on June 5, 2026 at 8:16 pm

    The use in the constructor might be safe but…

    • You should avoid using a bare new in a function call
    • The use of thr->join() in the destructor is… indicative of an issue

    In details…

    Do not use a bare new in function calls

    Since the order of execution of the operands is unspecified, it is not exception-safe. Prefer using std::make_shared here, or in the case of unique_ptr, create your own make_unique facility (with perfect forwarding and variadic templates, it just works); those builder methods will ensure that the allocation and the attribution of the ownership to the RAII class are executed indivisibly.

    Know the rule of three

    The rule of three is a rule of thumb (established for C++03) which says that if you ever need to write a copy constructor, assignment operator or destructor, you should probably write the two others too.

    In your case, the generated assignment operator is incorrect. That is, if I created two of your objects:

     int main() {
         xfer first;  // launches T1
         xfer second; // launches T2
    
         first = second;
     }
    

    Then when performing the assignment I lose the reference to T1, but I never ever joined it!!!

    I cannot recall whether the join call is mandatory or not, however your class is simply inconsistent. If it is not mandatory, drop the destructor; if it is mandatory, then you should be writing a lower-level class who only deals with one shared thread, and then ensure that the destruction of the shared thread is always preceeded by a join call, and finally use that shared thread facility directly in your xfer class.

    As a rule of thumb, a class that has special copy/assign needs for a subset of its elements should probably be split up to isolate the elements with special needs in one (or several) dedicated classes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the requirement to embed a Google Docs spreadsheet on a webpage, but
In our project we have requirement that, after receiving sms message from third party
I have requirement to get application cache object => session object, modify it and
I have a requirement to play YouTube video content from within an Android application
I have different requirement from a customer to write a automated test script for
I have a requirement that my app, can receive events (e.g. messages) from server
I have requirement to access the folder from a another machine based on the
I have a requirement to be be able to embed scanned tiff images into
I have requirement of creating MS Project object in asp.net c# and exporting image
we have requirement currently as shows in below image. have googled much but i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.