Results 1 to 2 of 2

Thread: [C++] zeromq thread safte closing

Threaded View

  1. #1
    Join Date
    May 2013
    Beans
    10

    [C++] zeromq thread safte closing

    Hi all
    I have a problem for closing a thread that implements the subscription side of a pub-sub pattern; I'm using zeromq 3.2 version.
    The code that implements the thread is:
    Code:
    ppsock->setsockopt(ZMQ_SUBSCRIBE, "001", 1);
        
        while (!thread_stop)
        {
            messaggio.clear();
            messaggio = s_recv(*ppsock);
            risposta.clear();
            risposta = s_recv(*ppsock);
            cout << risposta << endl;
            tok.clear();
            boost::split(tok, risposta, boost::is_any_of(";"));
            i = 0;
            for (vector<std::string>::iterator beg = tok.begin(); beg != tok.end(); ++beg)
            {
                //cout << std::to_string(i) << ": " << *beg << "\n";
                switch (i)
                {
                case 0:
                {
                    codice = std::stol(*beg);
                    break;
                }
                case 1:
                {
                    cmd = *beg;
                    break;
                }
                default:
                {
                    if (codice == ce_id)
                    {
                        if ((zone[i - 2] != std::stoi(*beg))&&(std::stoi(*beg) != -1))
                        {
                            zone[i - 2] = std::stoi(*beg);
                            sigZona(ce_id, i - 2);
                            itv = listacomandi.find(i-2);
                            if(itv!=listacomandi.end())
                            {
                                cout<<"Trovato comando su zona "<<std::to_string(itv->first)<<"Tipo: "<<std::to_string(itv->second)<<endl;
                                if(itv->second == 1)
                                {
                                    if (zone[i-2] == 1)
                                    {
                                        signComando(ce_id, 10, itv->first, 1, 1);
                                    }
                                }
                                if(itv->second == 2)
                                {
                                    if (zone[i-2] == 0)
                                    {
                                        signComando(ce_id, 10, itv->first, 2, 1);
                                    }
                                }
                            }
                        }
                    }
                }
                }
                i++;
            }
            if (!comandi.empty())
            {
                Urmet::eseguiComando();
            }
    For closing the thread I set the thread_stop variable and the join function. the problem is that s_recv function is blocking and i need to wait the next message for closing.
    Someone know a method for make s_recv non bloking or close subscription socket safety.
    Thanks
    Last edited by giane; June 7th, 2013 at 09:04 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •