Solved

Linux pthread programming....

Posted on 1999-01-18
3
593 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

15 Experts available now in Live!

Get 1:1 Help Now