Solved

Linux pthread programming....

Posted on 1999-01-18
3
599 Views
Last Modified: 2013-12-26
What am i doing wrong???

I think the output will be:

.1
.2
.1
.2
.1
.2
etc etc etc
.1
.1
.1
.1
^C

but its not happen...
--x--
#include <stdio.h>
#include <pthread/mit/pthread.h>

void *do_it (void *ptr);
void *do_it2 (void *ptr);

int main (void)
{
   int *pInt;
   pthread_t tid;
   pthread_t tid2;
   pthread_attr_t attr;
   struct sched_param sched;


   if (pthread_attr_init (&attr) == -1)
   {
      perror ("pthread_attr_init");
   }
   else
   {
      pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM);
      if (pthread_attr_getschedparam (&attr, &sched) == 0 )
      {
         sched.prio = 5;
         pthread_attr_setschedparam (&attr, &sched);
      }
   }
   if (pthread_create (&tid, &attr, do_it, (void *) pInt)== -1 )
      perror ("thread_create");
   if (pthread_create (&tid2, &attr, do_it2, (void *) pInt) == -1 )
      perror ("thread_create");

   for (;;)
      printf (".");
}

void *do_it (void *ptr)
{
   for (;;)
      printf ("1\n");
   pthread_exit(0);
   return NULL;
}

void *do_it2 (void *ptr)
{
   int i=0;

   for (i;i<10000;i++)
      printf ("2");

   pthread_exit(0);
   return NULL;
}

--x--
0
Comment
Question by:cox
  • 2
3 Comments
 
LVL 1

Expert Comment

by:sosedada
ID: 1296289
I'm guessing  1, 2 and . are printed out seemingly randomly.  This is because you have no guarantees as to when any particular thread is executing.  
This seems really annoying, but it isn't when you look at the big picture.  If the threads were not independent, there would be no advantage to them.  By allowing them to run on their own, the scheduler can make things run faster.  So, what you need to do is synchronize your threads.  Look into mutexes and semaphores for ways to do this.

Here is a good url:
http://dis.cs.umass.edu/~wagner/threads_html/tutorial.html
0
 
LVL 1

Author Comment

by:cox
ID: 1296290
but.... How can i do it works??

If i put

sleep (15);

and take off

for (;;)
   printf (".");

Its works, but the main (thread) is stoped... I dont want it..

is it possible?? How?
0
 
LVL 1

Accepted Solution

by:
sosedada earned 100 total points
ID: 1296291
Did you read the guide at the url I gave you?  It's just a start, but will give you more information than I feel like typing.

you will need two mutexes, one to control the operation of do_it and one to control do_it2.

this is pseudocode:
global mutex1, mutex2

main()
{
   lock both mutexes
   create threads

   for(;;)
   {
      unlock mutex1 //so do_it gets a chance to work
      delay //to let do_it work
      print .
      lock mutex1  //so do_it stops
   
      unlock mutex2
      delay
      print .
      lock mutex2
   }
}

do_it()
{
   for (;;)
   {
      lock mutex1   //wait for main to let us work
      print 1
      unlock mutex1 //we are done for this go 'round
      delay          //so main gets a chance to regain control
   }

   exit thread
   return
}

do_it2 is virtually identical to do_it

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question