SHARE
TWEET

Untitled

a guest Feb 18th, 2020 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <sched.h>    /* Only necessary for sched_yield */
  4. #include "string.h"
  5. #include "iostream"
  6.  
  7. using namespace std;
  8. void * print_message_function( void * VoidPtr )
  9. {
  10. //  char *message;
  11. //  message = (char *) VoidPtr;
  12. //  printf("%s", message);
  13. //  fflush(stdout);    /* Make sure we see it right away */
  14.     cout<<"print_message_function";
  15.  
  16.   /* If we wanted to return something, we would return a pointer
  17.    * to the data that we wanted to return.
  18.    *
  19.    * Instead of simply using return, we could also call
  20.    * pthread_exit.
  21.    */
  22.   return NULL;
  23. }
  24.  
  25. int main()
  26. {
  27.   pthread_attr_t    pthread_attributes;
  28.  
  29.   //pthread_t progressThread;
  30.     pthread_t thread1, thread2;
  31.     char *message1 = "Hello";
  32.     char *message2 = " World";
  33.     cout<<"main print"<<endl;
  34.    
  35.   /* Populate attributes with defaults
  36.    * If we wanted to customize this, we would probably
  37.    * first set the defaults and then change as need.
  38.    */
  39.   pthread_attr_init(&pthread_attributes);
  40.  
  41.   /* Start threads to write out messages
  42.    * Please note that we are not checking the return,
  43.    * this is something that you should figure out how
  44.    * to do on your own.
  45.    */
  46.   pthread_create( &thread1, &pthread_attributes,
  47.           &print_message_function, (void *) message1);
  48.    
  49.   /* Provide N secs for completion (very bad idea to rely on this)
  50.    * Use pthread_join instead, but you should learn to do this
  51.    * on your own.
  52.    */
  53.   //sleep(5);
  54.  
  55.   /* Note that this time we don't pass the pthread_attributes
  56.    * structure.  Instead we pass NULL which tells POSIX threads
  57.    * to just use the defaults.
  58.    */
  59.   pthread_create(&thread2, NULL,
  60.          &print_message_function, (void *) message2);
  61.  
  62.   /* Provide N secs for completion (very bad idea) */
  63.   //sleep(5);
  64.  
  65.   //exit(0);
  66. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top