[Operating System] {ud923} P2L3: Threads Case Study: PThreads

 

pthread_create create a new data structure that's of the type pthread type. "pthread_t *thread"

pthread_attr_t (p thread attribute type) is a data structure usde to specify certain things about the thread, that the p_thread library will take into consideration when managing the thread. (discuss in the next slide)

pthread_create return an "int", a status information, indicating the creation was successful or a failure. 

pthread_join also returns an "int", indicating the status

Thread   Pthreads

 Fork:

※ proc

※ args

pthread_create

※ start_routine

※ arg 

 Join  pthread_join

https://blog.csdn.net/Gordennizaicunzai/article/details/78007584

void** interpretation


 

https://docs.oracle.com/cd/E19504-01/802-5938/6i9lnfe4c/index.html

 

in Birrell's model, every thread has to join back to its parent thread.

But in pthread, there's a possibility for the children threads to be detached from the parent.

in this case, the children threads are kind of equivalent to the parent thread, with the exception that the parent thread holds some additional information about its children threads.

 

threads can also be created as detached threads, using the attribute DETACHED state.

the value of this attribute first needs to be set to PTHREAD_CREATE_DETACHED, because otherwise the default setting is to be  a joinable thread.

 

Errata

There is a typo @4:58 with the code example. The pthread_create() function should be passed the following parameters:

pthread_create(&tid, &attr, foo, NULL);

PThread Lesson Resources


https://stackoverflow.com/questions/22619876/openmp-multithreading-or-multiprocessing-c

 


https://stackoverflow.com/questions/3949901/pthreads-vs-openmp


 pthread hello-world example:

https://computing.llnl.gov/tutorials/pthreads/samples/hello.c

/******************************************************************************
* FILE: hello.c
* DESCRIPTION:
*   A "hello world" Pthreads program.  Demonstrates thread creation and
*   termination.
* AUTHOR: Blaise Barney
* LAST REVISED: 08/09/11
******************************************************************************/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS    5

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   printf("Hello World! It's me, thread #%ld!
", tid);
   pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
   pthread_t threads[NUM_THREADS];
   int rc;
   long t;
   for(t=0;t<NUM_THREADS;t++){
     printf("In main: creating thread %ld
", t);
     rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
     if (rc){
       printf("ERROR; return code from pthread_create() is %d
", rc);
       exit(-1);
       }
     }

   /* Last thing that main() should do */
   pthread_exit(NULL);
}

 

PThread Lesson Resources

 

 

up1+up2 => segmentation fault

up1+down2 => 1,2,3,4,5

down1+up2 => 4,5,5,5,5

donw1+down2 => 140732886074992,140732886074992,140732886074992,140732886074992,140732886074992

What's wrong??? 

 

 

 

 

 

 

 

 condition variable 

 

 

implementation: producer and consumer

 

 

Errata

Incase that it is unclear, the consumer never terminates because it operates in a continuous while(1) loop.


https://www.quora.com/Why-do-we-use-the-functions-fflush-stdin-and-fflush-stdout-in-c
 


summary

原文地址:https://www.cnblogs.com/ecoflex/p/10897665.html