LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-19-2012, 09:52 AM   #1
grob115
Member
 
Registered: Oct 2005
Posts: 542

Rep: Reputation: 32
Try for a configurable time and quit


Hi, am writing a C++ program that needs to execute a function, provided with a lib, that may hang resulting in the whole thread getting stuck. If I want to say wait for only a few seconds and then move on, is there a way to do this? Thanks.
 
Old 07-19-2012, 03:31 PM   #2
dmdeb
Member
 
Registered: Jul 2007
Location: Germany
Distribution: Debian
Posts: 45

Rep: Reputation: 6
Quote:
Originally Posted by grob115 View Post
Hi, am writing a C++ program that needs to execute a function, provided with a lib, that may hang resulting in the whole thread getting stuck. If I want to say wait for only a few seconds and then move on, is there a way to do this? Thanks.
Hi grob115,

now that sounds like a strange library function! Why does it behave like that? Is that really within its specification?

Anyway: You could create a new thread (man pthread_create), call that weird library function in that (using your own wrapper function), and monitor how that goes from your main thread. But to me, that sounds like a lot of work for just calling a library function...

Regards
dmdeb
 
Old 07-19-2012, 03:57 PM   #3
dmdeb
Member
 
Registered: Jul 2007
Location: Germany
Distribution: Debian
Posts: 45

Rep: Reputation: 6
Some example code for that (hanger.c):

Code:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

static int done = 0;

void obnoxious_library_function(void)
{
    if (random() % 2)
    {
        printf( "OK.\n" );
    }
    else
    {
        printf( "Hanging...\n" );
        sleep( 60 );
    }
}

void* my_wrapper(void *genarg)
{
    assert( genarg == NULL );

    done = 0;
    obnoxious_library_function( );
    done = 1;

    return NULL;
}

int main(void)
{
    for (;;)
    {
        pthread_t  thread;
        int        ret;

        ret = pthread_create( &thread, NULL, &my_wrapper, NULL );
        assert( ret == 0 );

        sleep( 1 );

        if (!done)
        {
            void  *result;

            ret = pthread_cancel( thread );
            assert( ret == 0 );

            ret = pthread_join( thread, &result );
            assert( result == PTHREAD_CANCELED );
        }
    }
}
Makefile example:

Code:
CFLAGS=-W -Wall -Werror -pedantic
LDFLAGS=-pthread
CC=gcc

all: hanger
But again: That library function behavior sounds really weird.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
So it felt like a good time to quit lurking... Andrew Decker LinuxQuestions.org Member Intro 1 07-26-2011 12:31 AM
capturing .TIFF images at a configurable time interval and sending to a usb drive DrZuhoch Linux - Newbie 3 05-04-2009 08:12 AM
Is DamnSmallLinux configurable? asilentmurmur DamnSmallLinux 8 08-02-2008 10:35 AM
firewall is not configurable alvi2 Linux - Networking 1 08-23-2005 03:44 PM
quit program after a specific time (prob. BASH) bglnelissen Linux - General 2 01-25-2005 01:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration