Sponsored Content
Full Discussion: Behavior of pthreads
Top Forums Programming Behavior of pthreads Post 302071344 by nhrraj on Saturday 15th of April 2006 12:09:00 PM
Old 04-15-2006
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
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

PThreads

Can anyone explain me how to use pthread_key_create() , pthread_setspecific(), pthread_getspecific() and pthread_key_delete () routines in pthreads. Kindly state by an example. (3 Replies)
Discussion started by: S.P.Prasad
3 Replies

2. Programming

pthreads

Does any one no of some good web site which will explain about how to program using pthreads in a UNIX enviroment? (6 Replies)
Discussion started by: fishman2001
6 Replies

3. Programming

pthreads

howcome that pthtreads spawn 2 extra processes? I'm kind of new with pthreads but fork() did not act like this. Anyone who can give me a technical explanation of what happends with mother / daughter processes? Best regards Esaia. (2 Replies)
Discussion started by: Esaia
2 Replies

4. Programming

PThreads

I have created a thread program, it is attached. My problem is that I need to loop this program multiple times, and basically reset everything including the threads created previously. I try to loop the program, the first run is fine, as always, but the second run of the program, the initialize... (0 Replies)
Discussion started by: justgotthis
0 Replies

5. UNIX for Dummies Questions & Answers

Question on Pthreads

How to give superuser privileges while setting the attributes like pthread_attr_setschedpolicy( )?? Even with normal user mode ,it is working fine for me.But in man pages, they have specied that to set the scheduling policy as SCHED_FIFO, the process should have superuser privileges. (0 Replies)
Discussion started by: yashavant.a
0 Replies

6. HP-UX

pthreads

Hi! I'm linking my hpux code using -lpthread (gcc), yet it references libpthread_tr.1, the debug version of the pthread lib. How do I force it to use pthreads? Thanks, :) (3 Replies)
Discussion started by: zackz
3 Replies

7. Programming

Problem with pthreads

hi i have a code: I found that after exiting from child thread memory isn't freed. I commented everything which is "some actions" here, so thread's function contains only two lines. But it doesn't help. What do I do wrong? Thanks a lot (3 Replies)
Discussion started by: sery0ga
3 Replies

8. UNIX for Advanced & Expert Users

Pthreads-Conditional variables

Hello, consider a bunch of threads which want to execute a while loop in parallel but after each iteration of while loop, we need a synchornization.That is, we have a global count varibale which is equal to zero at beginig of each iteration and at the end of each iterartion this variable... (0 Replies)
Discussion started by: Behnaz
0 Replies

9. Programming

MultiThreading using Pthreads

Situation: i have multiple pthread_create calls like this: pthread_create(...., ThreadFunc1,.....); pthread_create(...., ThreadFunc2,.....); . . which i am using to create multiple threads.All the "ThreadFunc<i>" functions are actually calling same function "Receive" of a class using same... (3 Replies)
Discussion started by: Sastra
3 Replies

10. Programming

Idleness with pthreads

Hello, I am writing a threaded program using the pthread library in linux. I have a master thread that needs to control worker threads, in particular, it has to be able to ensure that none of the worker threads will be running for a specified amount of time, even if the worker threads are... (7 Replies)
Discussion started by: andres625
7 Replies
ASSERT_PERROR(3)					     Linux Programmer's Manual						  ASSERT_PERROR(3)

NAME
assert_perror - test errnum and abort SYNOPSIS
#include <assert.h> void assert_perror(int errnum); DESCRIPTION
If the macro NDEBUG was defined at the moment <assert.h> was last included, the macro assert_perror() generates no code, and hence does nothing at all. Otherwise, the macro assert_perror() prints an error message to standard output and terminates the program by calling abort() if errnum is nonzero. The message contains the filename, function name and line number of the macro call, and the output of str- error(errnum). RETURN VALUE
No value is returned. CONFORMING TO
This is a GNU extension. BUGS
The purpose of the assert macros is to help the programmer find bugs in his program, things that cannot happen unless there was a coding mistake. However, with system or library calls the situation is rather different, and error returns can happen, and will happen, and should be tested for. Not by an assert, where the test goes away when NDEBUG is defined, but by proper error handling code. Never use this macro. SEE ALSO
exit(3), abort(3), assert(3), strerror(3) GNU
2002-08-25 ASSERT_PERROR(3)
All times are GMT -4. The time now is 12:53 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy