The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
pthreads zackz HP-UX 3 01-24-2007 02:03 AM
PThreads justgotthis High Level Programming 0 10-08-2005 03:01 PM
pthreads Esaia High Level Programming 2 06-04-2003 06:32 PM
pthreads fishman2001 High Level Programming 6 06-25-2002 05:01 AM
PThreads S.P.Prasad UNIX for Advanced & Expert Users 3 06-06-2002 11:23 AM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 04-15-2006
nhrraj nhrraj is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 2
Behavior of pthreads

Hi All,
I ve written a small program to get started off with pthreads. I somehow feel the program doesnt meet the purpose. Please find the code and the output below. Please find my question at the bottom.

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

void *PrintThread1(void *threadid)
{
  int i;
  for (i=0;i<50;i++)
  {
    printf("\n%d: Thread1!\n", threadid);
  }
}
void *PrintThread2(void *threadid)
{
    int i;
    for (i=0;i<50;i++)
    printf("\n%d: Thread2!\n", threadid);
    pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
  pthread_t threads, threads1;
  int rc, t1=0, t2=1;

  printf("Creating thread %d\n", t1);
  rc = pthread_create(&threads, NULL, PrintThread1, (void *)t1);
    if (rc)
    {
      printf("ERROR; return code from pthread_create() is %d\n", rc);
    exit(-1);
    }
  printf("Creating thread %d\n", t2);
  rc = pthread_create(&threads1, NULL, PrintThread2, (void *)t2);
    if (rc)
    {
      printf("ERROR; return code from pthread_create() is %d\n", rc);
    exit(-1);
    }

  pthread_exit(NULL);
}
Output
--------
0: Thread1! (printed when i=0)

0: Thread1!

...............

...............

0: Thread1! (printed when i=49)

1: Thread2! (printed when i=0)

1: Thread2!

-----------

-----------

1: Thread2! (printed when i=49)


My question is:
If both the threads are running the output from the functions printThread1 and PrintThread2 should be mingled?
i am in HP-UX 10.20
Same program in cygwin environment yields mingled output.
Please help me with this...

raj
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:40 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0