All pastes #42736 Raw Edit

Someone

public text v1 · immutable
#42736 ·published 2006-02-22 04:40 UTC
rendered paste body

#include <stdio.h>
#include <assert.h>
#include <pthread.h>

#define FALSE 1

struct test_data {
        char *name;
        char *arg;
};

void  *testThreadRunner (void *args )
{
        printf("testThreadRunner started\n");
        struct test_data *myData = args;
        sleep(200);
        free(myData);
        printf("testThreadRunner ended\n");
        return;
}


int main ( void )
{

        pthread_t myThread;
        pthread_attr_t myAttr;
        struct test_data *mydata;

        mydata = malloc(sizeof(struct test_data));
        memset(mydata, 0, sizeof(struct test_data));

        char *name = "test";
        char *arg = "--yup";

        int pthreadAttrInit = pthread_attr_init(&myAttr);
        printf("%d\n", pthreadAttrInit);
        assert(pthreadAttrInit != FALSE);

        int pthreadAttrSetSchedPolicy = pthread_attr_setschedpolicy(&myAttr, SCHED_RR);
        assert( pthreadAttrSetSchedPolicy != FALSE);

        int pthreadAttrSetDetachedStateRes  = pthread_attr_setdetachstate(&myAttr, PTHREAD_CREATE_DETACHED);
        assert( pthreadAttrSetDetachedStateRes != FALSE);

        int pthreadCreateRes = pthread_create(&myThread,  &myAttr, &testThreadRunner, &mydata);
        assert (pthreadCreateRes != FALSE);

        int pthreadAttrDestroyRes = pthread_attr_destroy(&myAttr);
        assert( pthreadAttrDestroyRes != FALSE);

        int timeout = 30;
        printf("All success, starting sleeping for %d seconds", timeout);
        int i = 0;
        for (i = 0; i < timeout; i++ )  {
                sleep(1);
                printf(".");
        }
        printf("Done sleeping, sending pthread_cancel\n");

        int pthreadCancel = pthread_cancel(myThread);
        assert( pthreadCancel != FALSE );

        return -1;

}