Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Linux pthread programming....

Posted on 1999-01-18
3
Medium Priority
?
617 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 400 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

963 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