CopernicaMarketingSoftware / PHP-CPP

Library to build PHP extensions with C++

Home Page:http://www.php-cpp.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

do a job in back after user disconnect

parsibox opened this issue · comments

hi
i need create a function that call another function in background and do some job
in main function i need do return and background my job continue
i create a thread and t.detach(); that to be on background but it not work and we get Segmentation fault

this is my code please hel me:

#include <iostream>
#include <phpcpp.h>
#include <thread>
#include <fstream>
#include <string>
using namespace std;

void TelircoBack(const std::string& queue_name, const std::string& json_str)
{

    std::string file_path = "/var/www/c" + queue_name;
    std::ofstream file(file_path);
    if (file.is_open()) {
        file << json_str;
        file.close();
    }
}

Php::Value insert_json_to_queue(Php::Parameters& params)
{
    std::string queue_name = params[0];
    std::string json_str = params[1];


    // Call the TelircoBack function in a separate thread
 std::thread t(TelircoBack, queue_name, json_str);
 t.detach();
    return queue_name;
}

extern "C" {
    PHPCPP_EXPORT void *get_module() 
    {
        static Php::Extension extension("TelircoAMPQ", "1.0");

        // Register the insert_json_to_queue function
        extension.add<insert_json_to_queue>("insert_json_to_queue", {
            Php::ByVal("queue_name", Php::Type::String),
            Php::ByVal("json_str", Php::Type::String)
        });

        return extension.module();
    }
}

dear please help me

This seems to be a generic C++ question on how you start threads, not so much PHP-CPP related.