Solved

Linux pthread programming....

Posted on 1999-01-18
3
590 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now