[En-Nut-Discussion] Thread is interrupted by another thread and doesn't come back.

Petr Odlozil zip357 at centrum.cz
Tue Nov 28 15:07:49 CET 2006


Hi Harald,


 > Last not least I can't see any reason for using two threads.

  I will check if it is necessary to have 2 threads. But the reason why 
I was doing this was, that the server I communicate with supports only 
one created connection and I keep this connection open in SEND THREAD. 
And to this connection I only resend data received from those accepted 
connections. There may be more computers, which want to send queries to 
the server.
When I did first tests it took too long to open new connection with the 
server for every request. (I have ethernut 1.3 - charon)

> If the savetoque doesn't return ERRQUE_FULL you don't close
> the old socket, but create a new one. Sooner or later you
> run out of memory, NutTcpCreateSocket will fail and the
> loop (without any NutSleep, NutEventWait or similar) will
> block the complete system.

I close the serversocket created in RECEIVE in the end of SEND thread, 
just after the finish of the while loop. I can't close it in RECEIVE 
thread, because SEND thread use this socket for communication.

> NutHeapAvailable() helps to verify the number of bytes used
> by the program.

Yes I have this there and the Heap is ok.

> The THREAD SEND sets a timeout on serversock, but how can it
> be sure that it is created?

The serversock was created in RECEIVE thread (CreateSocket and Accept) 
ended correctly. If it was correctly created I put here the pointer to 
the serversocket to que. There are only correctly created and accepted 
sockets in the que.

Best Regards

Petr

> Hi Petr,
> 
> If the savetoque doesn't return ERRQUE_FULL you don't close
> the old socket, but create a new one. Sooner or later you
> run out of memory, NutTcpCreateSocket will fail and the
> loop (without any NutSleep, NutEventWait or similar) will
> block the complete system.
> 
> Also check the same in case of timeouts.
> 
> NutHeapAvailable() helps to verify the number of bytes used
> by the program.
> 
> The THREAD SEND sets a timeout on serversock, but how can it
> be sure that it is created?
> 
> Last not least I can't see any reason for using two threads.
> 
> Harald
> Harald
> 
> 
> At 08:42 28.11.2006 +0100, you wrote:
>> Hello,
>>
>>   I'm kindly asking you for the help.
>>
>>   We're building some kind of a converter, which receives data from a
>> client and resends them to a server, then receives data from this server
>> and resends them to the connected client.
>>
>> There are two threads which accept connections and save sockets to the
>> que. (After the mail there is a simplified code ...)
>> Then there is one thread which does the communication. In the while loop
>> gets sockets from the que, receives data and does the resending.
>>
>> What happens is that the receiving thread interrupts the sending thread
>> somewhere in NutTcpReceive() or NutTcpSend().
>>
>> And even if there is no conection comming .. so the NutTcpAccept is just
>> blocking the receive thread, it doesn't go back to the send thread. So
>> the SEND hangs in the while loop. This happens just every other day.
>>
>> Most of the time the code works fine.
>>
>> I would really appreciate your help.
>>
>> Best regards
>>
>> Petr Odlozil
>> -------------------------------------------------------------
>> static u_long timeout = 500
>>
>> THREAD RECEIVE - receiving commands
>>
>> for(;;)
>> {
>>   if ((serversock=NutTcpCreateSocket()) != 0)
>>    {
>>         if( NutTcpAccept(serversock,commandport)==0) {
>>
>>         buff[0] = (((unsigned int)serversock) & 0x00FF);
>>         buff[1] = ((((unsigned int)serversock) & 0xFF00) >> 8);
>>         if (savetoque(buff,2,&sendque)==ERRQUE_FULL)
>>         {
>>            NutTcpCloseSocket(serversock);
>>         }
>>         NutEventPostAsync(&queries_comming);
>>         }
>>    }
>> }
>>
>> THREAD SEND --- resending commands
>> for(;;)
>> {
>> if (readfromque(pombuff,&n,0,&sendque) == ERRQUE_NOERR)
>> {
>>   // here is getting the serversock from pombuff and opening connection
>> the clientsock
>>   // then I go to receive data from serversock sending them to client
>> sock, receive replies and sending them to serversock.
>>
>> NutTcpSetSockOpt(serversock,SO_SNDTIMEO,&timeout,sizeof(timeout));
>> NutTcpSetSockOpt(serversock,SO_RCVTIMEO,&timeout,sizeof(timeout));
>>
>> //***somewhere in this cycle it is interrupted by the thread RECEIVE***
>> and it doesn't come back to this thread
>>
>> while ((!NutTcpError(serversock)) && (!NutTcpError(clientsock)))
>> {
>>     if ((bytes= NutTcpReceive(serversock,buff,commandsize))<0)
>>         {       break;
>>         };
>>      if ((errcode=NutTcpSend(clientsock,buff,bytes))<bytes)
>>         {         break;
>>         }
>>      if ((bytes = NutTcpReceive(clientsock,buff,commandsize))<0)
>>         {       break;  };
>>      if ((errcode=NutTcpSend(serversock,buff,bytes))<bytes)
>>         {  break; }
>> }
>>
>> //if I finish everything from serversock I close sockets and wait for
>> the event ...
>> NutEventWait(&queries_comming,0);
>> }
>> _______________________________________________
>> En-Nut-Discussion mailing list
>> En-Nut-Discussion at egnite.de
>> http://lists.egnite.de/mailman/listinfo/en-nut-discussion
> 
> _______________________________________________
> En-Nut-Discussion mailing list
> En-Nut-Discussion at egnite.de
> http://lists.egnite.de/mailman/listinfo/en-nut-discussion
> 
> 



More information about the En-Nut-Discussion mailing list