4

I have a PHP script where I create several threads with the pthread library. In these threads I use the sleep() function. The problem is that sleep() pause my main thread. So I lose all the advantage of making threads...

Does anyone have a way to pause my threads without pausing my main thread?

This threaded class call another php script where the sleep is.

class CallAgent extends Thread
{

private $id_com;
private $agt_id;
private $pat_id;
/**
 * CallAgent constructor.
 */
public function __construct($id_com, $agt_id, $pat_id)
{
    $this->id_com = $id_com;
    $this->agt_id = $agt_id;
    $this->pat_id = $pat_id;
}

public function run()
{
    echo shell_exec("C:\\xampp\php\php.exe -f C:\\xampp\htdocs\call_script.php -- " .$this->id_com. " " .$this->agt_id. " " .$this->pat_id);
}
}

This is the script called by the thread.

function run($id_com, $agt_id, $pat_id)
{
$bdd = new bdd();
$bdd->connect();
$decision = rand(0, 100);
if ($decision >= 2) {
    echo "L'agent à décrocher et rentre en com avec un patient\n";
    $bdd->update_etat_com($id_com, "decrocher");
    $bdd->update_agt_etat($agt_id, "com");
    $bdd->update_pat_etat($pat_id, "OUT");
    sleep(8);
    echo "Call finished.\n";
    $bdd->update_agt_etat($agt_id, "repos");
}
else {
    $bdd->update_dispo_agt($agt_id, "indisponible");
    echo "Timeout Agent.\n";
}
}

Thanks in advance.

Hcetipe
  • 466
  • 2
  • 10

0 Answers0