LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 05-27-2003, 12:34 AM   #1
rch
Member
 
Registered: Feb 2003
Location: Santa Clara,CA
Distribution: Mandriva
Posts: 909

Rep: Reputation: 48
Pthread


Code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <thread.h>

void* servant( void* );

#define FALSE 0
#define TRUE  1

#define MAX 30

static int count;
static int list1[MAX];
static int list2[MAX];

/******************************************************************************
  Function main

  Initialize variables and create two additional threads, then wait for the
  servant threads to complete.
******************************************************************************/

int main()
{
  pthread_t mythread[2];
  int i;

  pthread_setconcurrency( 3 );

  count = 0;
  for (i=0; i<MAX; i++)
  {
    list1[i] = 0;
    list2[i] = 0;
  }

  for (i=0; i<2; i++)
  {
    if (pthread_create( &mythread[i], NULL, servant, (void*) i ))
    {
      printf( "*** Error creating thread ***\n" );
      exit( -1 );
    }
  }

  for (i=0; i<2; i++)
  {
    if (pthread_join ( mythread[i], NULL ))
    {
      printf( "*** Error joining thread ***\n" );
      exit( -2 );
    }
  }

  printf( "\nFinal contents of arrays:\n\n" );
  for (i=0; i<MAX; i++)
  {
    printf( "  %2d:  %d  %d", i, list1[i], list2[i] );

    if (list1[i] == 0 || list2[i] ==0 || list1[i] != list2[i])
    {
      printf( " *** error ***" );
    }
    printf( "\n" );
  }
  printf( "\nFinal value of count: %d\n\n", count );

  return 0;
}

/******************************************************************************
  Function servant

  Each servant thread loops MAX times, incrementing the count by 1
  and placing its thread ID into two parallel arrays.
******************************************************************************/

void* servant( void* arg )
{
  pthread_t tid = pthread_self();
   int i;
  for (i=0; i<MAX; i++)
  {
    if (count < MAX)
    {
      list1[count] = tid;

      if ((random()&0x00000003) == 0x00000003) thr_yield();

      list2[count] = tid;

      count = count + 1;
    }
  }

  return NULL;
}
replacing thread.h(solaris) for sched.h and thr_yield(solaris) for sched_yield why the value of count comes to 31?
 
Old 05-28-2003, 02:20 AM   #2
yrraja
Member
 
Registered: Sep 2002
Distribution: RH, FC, Ubuntu, Solaris, AIX
Posts: 114

Rep: Reputation: 15
One problem that i see in this code is that you have launched same function in 2 threads which uses same global variables. This will result in abnormal results. You must use Mutexes or Semaphores in this function.
 
  


Reply



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
Pthread JanusPaul Programming 8 02-09-2005 05:46 PM
pthread zaman Programming 6 08-29-2004 04:04 PM
pthread and C++ gt73 Programming 17 11-04-2003 03:01 AM
about pthread c12ayon Programming 6 10-25-2003 04:37 AM
need help about pthread c12ayon Programming 1 10-24-2003 07:47 AM

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

All times are GMT -5. The time now is 01:28 PM.

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