Solved

Linux pthread programming....

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Title # Comments Views Activity
Detect CR LF to each line 12 171
WinWaitActive parameters 12 31
no14 challenge 14 72
Annoying "thing" blocks my view 4 117
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 …
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 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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

820 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