I have a very recent version of Cygwin (see cygcheck.out attached) and an test 
application (attached) which has been extracted from the SIPP application. 

What it does in a loop is

*         Creates a thread

*               In the thread: Calls "select" to wait for just under 1 second 
the exits the thread

*         Waits for the thread to exit.


And every time it goes around the loop, according to windows task manager, it 
one more handle is being used. 

If the call to "select" is removed, and a sleep of 1 is put in the loop, then 
it doesn't leak handles.

Hence my question, is "select" thread safe?

Regards
Trefor Edwards 
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <pthread.h>

void *send_wrapper(void *arg);

main()
{
        unsigned int i;
        int ret;
        
    pthread_t media_thread;
    
    media_thread = 0;

    printf("Starting");
        for ( i=0 ; i<1000 ; i++ )
        {
                printf("Loop %d\n", i);        
            /* Create a thread to send RTP packets */
        pthread_attr_t attr;
        pthread_attr_init(&attr);
/*
        if (media_thread != 0) 
        {
                        printf("cancelling\n");
                // If a media_thread is already active, kill it before starting 
a new one
            pthread_cancel(media_thread);
            pthread_join(media_thread, NULL);
            media_thread = 0;
        }
*/
        int ret = pthread_create(&media_thread, &attr, send_wrapper,
                                     (void *) 0);
        if(ret) printf("Can create thread to send RTP packets");

        pthread_attr_destroy(&attr);


        pthread_join(media_thread, NULL);
        media_thread = 0;
#ifdef NO_SELECT      
        sleep(1);
#endif

        }
}


void *send_wrapper(void *arg)
{
    
        struct timeval tv;
        int result=0;

/*
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
*/

        tv.tv_sec = 0;
    tv.tv_usec = 900 *  1000;

        printf("Select ");
#ifndef NO_SELECT      
        result = select(0, (fd_set *)NULL, (fd_set *)NULL, (fd_set *)NULL, &tv);
#endif
        if (result!=0) printf("Result %d\n", result);
    
        pthread_exit(NULL);
    return NULL;
}

Attachment: cygcheck.out
Description: cygcheck.out

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

Reply via email to