Pthreads - Need help


Results 1 to 2 of 2

Thread: Pthreads - Need help

  1. #1
    Join Date
    Jul 2006
    Posts
    10

    Pthreads - Need help

    I am new to Pthreads. I had write the following program contains 2 threads one is write data and the other read data. The codes compile but the output is not good because read thread take too much time and no share resource for write. Could any one tell me what is wrong below codes?

    thank you in advance

    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <string.h>
    #include <list.h>
    #include <signal.h>
    #include <errno.h>
    #include <aio.h>
    #include <time.h>

    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/sem.h>
    #include <sys/file.h>

    using std::list;

    #define SEM_KEY 4567
    #define MAX_FRAMES 100000
    #define MAX_BUFFER_SIZE 1024

    union semun
    {
    int val;
    struct semid_ds *buf;
    unsigned short *array;
    };

    volatile int noWrite;
    volatile bool bWriteThreadExited = false;

    list <char *> bufferList;
    int out_file;

    pthread_mutex_t bl_mutex = PTHREAD_MUTEX_INITIALIZER;

    void *writeThreadFunction(void *arg);
    void *readThreadFunction(void *arg);

    char writeThreadMessage [256] = "Write Thread";
    char readThreadMessage[256] = "Read Thread";


    void SleepInMiliseconds(unsigned int mseconds)
    {
    pthread_mutex_lock(&bl_mutex);
    clock_t goal = mseconds + clock();
    while ( goal > clock());
    pthread_mutex_unlock(&bl_mutex);
    }

    int main (void)
    {
    time_t the_start_time;
    time_t the_end_time;

    noWrite = 0;

    if ((out_file = open("testdata.txt", O_WRONLY|O_CREAT|O_LARGEFILE, 0777)) == -1)
    {
    exit(errno);
    }

    int result;
    pthread_t writeThread;
    pthread_t readThread;
    void *writeThreadResult;
    void *readThreadResult;
    the_start_time = time((time_t *)0);

    result = pthread_create(&writeThread, NULL, writeThreadFunction, (void *)writeThreadMessage);
    if ( result != 0 )
    {
    perror("write thread creation failed");
    exit(EXIT_FAILURE);
    }
    result = pthread_create(&readThread, NULL, readThreadFunction, (void *)readThreadMessage);
    if ( result != 0 )
    {
    perror("read thread creation failed");
    exit(EXIT_FAILURE);
    }
    printf("Waiting for write thread to finish...\n");
    result = pthread_join(writeThread, &writeThreadResult);
    if ( result != 0 )
    {
    perror("Thread join failed");
    exit(EXIT_FAILURE);
    }
    printf("Waiting for read thread to finish...\n");
    result = pthread_join(readThread, &readThreadResult);
    if ( result != 0 )
    {
    perror("Thread join failed");
    exit(EXIT_FAILURE);
    }
    printf("buffer list size is: %d\n", bufferList.size());
    printf("Write Thread joined, it returned %s\n", (char *)writeThreadResult);
    printf("Read Thread joined, it returned %s\n", (char *)readThreadResult);

    printf("Write Thread Message is now : %s\n", writeThreadMessage);
    printf("Read Thread Message is now : %s\n", readThreadMessage);
    fsync(out_file);
    close(out_file);
    the_end_time = time((time_t *)0);
    printf("Closed Output File\n");
    double timeused = (double)(the_end_time - the_start_time);
    printf("Total time use to copy %d second(s)\n", timeused );
    pthread_mutex_destroy(&bl_mutex);
    exit(EXIT_SUCCESS);
    }

    void * writeThreadFunction(void *arg)
    {
    while ( noWrite < MAX_FRAMES )
    {
    noWrite++;
    char * pszBuffer = (char *) malloc(MAX_BUFFER_SIZE+1);
    memset(pszBuffer, 'A', MAX_BUFFER_SIZE);
    pthread_mutex_lock(&bl_mutex);
    printf("Write Thread\n");
    bufferList.push_back(pszBuffer);
    printf("Write Thread - list size is : %d\n", bufferList.size());
    pthread_mutex_unlock(&bl_mutex);
    //SleepInMiliseconds(1);
    }
    bWriteThreadExited = true;

    strcpy( writeThreadMessage, "write thread Bye!" );
    pthread_exit( (void *)"Write thread - thank you for the CPU time" );
    }

    void* readThreadFunction(void *arg)
    {
    bool exitReadThread = false;
    while ( exitReadThread == false )
    {
    pthread_mutex_lock(&bl_mutex);
    printf("Read Thread\n");
    if ( bWriteThreadExited && bufferList.empty() )
    exitReadThread = true;
    if ( !bufferList.empty() )
    {
    char * pszBuffer = (char *) bufferList.front();
    bufferList.pop_front();
    printf("Read Thread - list size is : %d\n", bufferList.size());
    }
    pthread_mutex_unlock(&bl_mutex);
    //SleepInMiliseconds(1);
    }
    strcpy(readThreadMessage, "read thread Bye!");
    pthread_exit((void *)"Read thread - thank you for the CPU time");
    }

  2. #2
    Join Date
    May 2001
    Location
    Uh, I'm somewhere where I don't know where I am.
    Posts
    1,228
    For one, it's not entered as code, making it a PITA to read and help debug.
    if (i_forgot && this_is_about_code)
    language = c++;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •