How can I communicate between threads

How can I create threads in C ++ and communicate between the main thread and another?

Some code examples will be appreciated.

+5
source share
4 answers

Potential thread variables with their scope, so mutex locks are so important.

This way, you can easily communicate simply by editing a variable common to both threads:

#include <iostream>
#include <pthread.h> 
main()  {
    pthread_t f2_thread, f1_thread; 
    void *f2(), *f1();
    int i1;
    i1 = 1;
    pthread_create(&f1_thread,NULL,f1,&i1);
    pthread_create(&f2_thread,NULL,f2,&i1);
    pthread_join(f1_thread,NULL);
    pthread_join(f2_thread,NULL);
}
void *f1(int *x){
  std::cout << *x << std::endl;
}
void *f2(int *x){
  sleep(1)
  std::cout << ++(*x) << std::endl;
}

This should be printed:

1
2

And the variable i1 was split between threads. This is one form of communication, you can share the structs strings classes, whatever you want.

NOTE. This code will almost certainly cause a thread race. This is just an example; you should always use synchronization and thread methods when sharing memory between threads.

+2

( , ), std::thread, ++ 11 boost::thread , .
  boost::asio::io_service, , , , , - , .

+1

, : _beginthread

  • process.h, .
  • .
  • (void *).
  • _endthread(), .

pi. pi, , . () , pi. .

      #include<process.h>
      #include<windows.h> //for the Sleep()
      #include<stdio.h>
      volatile boolean found=false;
      volatile int who=0;

      void find_pi_core1(void * iterations)
      {

          int * ite=(int *)iterations;
          for(int i=0;i<ite;i++)
          {
              //a sample of Monte-Carlo method here to find pi 
              //gets pi with more precision with additional iterations
               if(found) _endthread();
          }
          found=true;who=1;
          _endthread();    

       }

       void find_pi_core2(void * iterations)
       {

           int * ite=(int *)iterations;
           for(int i=0;i<ite;i++)
           {
                //a sample of Monte-Carlo method here to find pi 
                //gets pi with more precision with additional iterations
                 if(found) _endthread();
           }
           found=true;who=2;
          _endthread();
      }

       void printscreeN(void * dummy)
       {

          while(!found)
          {
              Sleep(30);      //if dont sleep, it gets a lot of cpu power
          }
          printf(" found! \n" );
          _endthread();
      }

      int main()
      {

                      Function name
                          ^       Stack size
                          |          ^           Function parameter
                          |          |             ^
          _beginthread(find_pi_core1,0,(void *) 3000000);
          _beginthread(find_pi_core2,0,(void *) 2500000);
          _beginthread(printscreeN,0,(void *)0);
          Sleep(3000);
          if(found)
          { 
               printf("pi has been found! core-%d has found pi first ",who);
          }
          else
          {printf("pi has not been bound!");}
          return 0;
      }
0

.

Linux pthread library pthread_create pthread_join .

Windows Windows API CreateThread() WaitForSingleObject(), .

-1

All Articles