UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2007-02-02 04:10 AM

frank2004
Member
From: Beijing
Registered: 2004-07-20
Posts: 93

Re: thread hang up ?

In my multi-thread application, on one node, I used one thread (send thread)to send my defined heart-beat packets via tcp to the other node.The interval is 3 seconds. At the other node, there is a thread(received thread) to received the Heart-beat, sometimes the received thread can not received the packets, I check the send thread, it really does not send, becase once the packets sendout, there incease the packet counter, when the above case happened, the packet counter does not changed. In the send thread, I used pthread_yield() call before delay 3''(pthread_delay_np), Is it possible that the pthread_yield caused thread give up CPU and can not been scheduled in time? The send thread structure looks the following:

void send_thread(void)
{
...
   while (1)
    {
        ...
        my_database_access();
        ..
       ret=send(fd,buffer,len);
      ..
      pthread_yield();
      pthread_delay_np(&interval);
}
}

  I want to comment out the pthread_yield() to fix the problem, useful? Thanks.Oh, by the way, the application running on the HP True64 UNIX (Alpha server).

Offline

#2 2007-02-02 10:17 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: thread hang up ?

my_database_access(); can probably take some time as well. Not sure what the yield is supposed to do, you're giving the CPU free anyway when doing the delay. Just measure the exact delay you need to sleep just before doing the pthread_delay_np(), instead of assuming everything before it takes no time.

Offline

#3 2007-02-03 08:03 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: thread hang up ?

Um... First things first...

When you call pthread_yield() you are telling the system that it is OK to switch context and allow another thread ( or process ) gain control of the CPU... the call is typically used in heavy CPU bound threaded code to help prevent starvation of other threads / processes on the system...

There is no guarantee with a pthread_yield() that the thread will give up the CPU... AND there is no guarantee that, if it does give up the CPU, how long an interval of time will pass before it regains it - other than that specified by the current system ( or if present - second level ) scheduler...

As for the pthread_delay_np()... that np means non-portable... if you want to write portable code and emulate it... create a mutex... create a thread... have the thread lock the mutex and return / exit without unlocking the mutex... then create your thread(s) you want to delay and do a pthread_mutex_timedlock( abs delay time ) call against it...

One last comment on the pthread_yield call... if you do have code where it would be useful... pthread_yield is superceded by the sched_yield function in the current specs...


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#4 2007-02-05 03:19 AM

frank2004
Member
From: Beijing
Registered: 2004-07-20
Posts: 93

Re: thread hang up ?

I think my_database_access() call will not take enough time at least in the thread, it just only gets the matched record and updates its one attribute  value in our realtime database, then the database is implemented by shared memory.
  What I want is to make sure the thread can do the above operation and send the tcp packet after each time of 3 seconds, no matter how long the above operation will take.
  Yes, the same function of pthread_yield has been replaced by the sched_yield() on some current system,but in our system, at least this call is still been supported.
   why the pthread_yield is commented out is there is no dead loop to cause the tcp packet not to send out except the CPU scheduler?!

Offline

#5 2007-02-05 10:18 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: thread hang up ?

If you want any timing guarantuees whatsoever, then don't use any form of yield.

Offline

Board footer

Powered by FluxBB