QNX multi-threaded synchronization barrier (barrier)

Source: Internet
Author: User

Before and we introduced the method of thread synchronization on QNX Metux and Semophore, two methods can be used to lock one or several resources to avoid conflict of resource usage. In another case, a thread needs to use the thread synchronization method, barrier, to continue execution after another thread has finished working.

To give a realistic example, suppose there are three soldiers operating an antiaircraft gun, one responsible for filling shells, one is responsible for adjusting the gun around the direction, a responsible for adjusting the angle of the antiaircraft gun. It is obvious that an antiaircraft gun needs to be fired after three soldiers have completed their mission, otherwise they will either be banned or the shells will never get out.

One solution is to give an antiaircraft gun set three buttons, only three buttons are pressed to launch the anti-aircraft, so no matter which soldiers to complete the task, he only need to press his own button, once the three buttons are pressed, indicating that three soldiers have completed the work, fired shells.

This three-button device can be said to be a barrier to ensure that the AA can work properly.

Let's take this example to do a barrier test.

First, you need to introduce a header file, including:

#include <stdlib.h>

#include <stdio.h>

#include <pthread.h>

#include <sync.h>

#include <sched.h>

#include <unistd.h>

Where Pthread.h and sync.h are key, one for thread creation and one for barrier.

Then define a pthread_barrier_t global variable:

pthread_barrier_t barrier;

Then initialize the barrier in the main function:

Pthread_barrier_init (&barrier, NULL, 3);

The first parameter is the barrier variable, the third parameter is the number of waits for the barrier, this example is waiting for 3 soldiers to complete a task, so set to 3.

Then start the thread of three soldiers, each soldier thread is responsible for completing a work, the completion of a delay before the cycle, indicating the time required to complete the work, different soldiers thread delay time is different, indicating that the completion of different tasks need different time.

After all the soldier threads have finished working, call the Pthread_barrier_wait method, notify barrier to complete, and start waiting, calling the method as follows, the only parameter being the initialized barrier variable:

pthread_barrier_wait (&barrier);

Once the pthread_barrier_wait execution completes, indicating that the work used is complete, the firing of shells, by printing "fire!" Said.

printf ("Soldierx.::::::::: fire! \ n ");

In addition, the main thread initiates three soldier threads and then enters a loop wait while outputting some time information.

The results of the program execution are as follows:

Entering Barrier Test

Creating the Barrier

Creating the Threads

Soldier1:::::::::: Adjusting angle

Soldier2:::::::::: Adjusting direction

Soldier3:::::::::: Filling Bullet

Timer in main thread:0

Timer in main thread:1

Timer in main thread:2

Timer in main thread:3

Timer in main thread:4

Soldier1:::::::::: Angle ready!

Timer in main thread:5

Soldier2:::::::::: Direction ready!

Timer in main thread:6

Timer in main thread:7

Soldier3:::::::::: Bullet ready!

Soldier3:::::::::: fire!

Soldier1:::::::::: fire!

Soldier2:::::::::: fire!

Timer in main thread:8

Timer in main thread:9

End of Main thread

As you can see, only three threads have completed the task before they continue to execute, ensuring that the cannon can be launched successfully.

The complete code is as follows:

[CPP]View PlainCopy
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <pthread.h>
  4. #include <sync.h>
  5. #include <sched.h>
  6. #include <unistd.h>
  7. pthread_barrier_t barrier;
  8. void Soldier1 () {
  9. printf ("Soldier1::::::::: Adjusting angle \ n");
  10. int i = 0;
  11. For (i = 0; i <; i++) {
  12. Delay (45);
  13. }
  14. printf ("Soldier1::::.:::: Angle ready!\n");
  15. Pthread_barrier_wait (&barrier);
  16. printf ("Soldier1.:::::::::fire!  \ n ");
  17. }
  18. void Soldier2 () {
  19. printf ("Soldier2::::::::: Adjusting direction \ n");
  20. int i = 0;
  21. For (i = 0; i <; i++) {
  22. Delay (55);
  23. }
  24. printf ("Soldier2::::.:::: Direction ready!\n");
  25. Pthread_barrier_wait (&barrier);
  26. printf ("Soldier2.:::::::::fire!  \ n ");
  27. }
  28. void Soldier3 () {
  29. printf ("Soldier3::::::::: Filling bullet \ n");
  30. int i = 0;
  31. For (i = 0; i <; i++) {
  32. Delay (76);
  33. }
  34. printf ("Soldier3::::.:::: Bullet ready!\n");
  35. Pthread_barrier_wait (&barrier);
  36. printf ("Soldier3.:::::::::fire!  \ n ");
  37. }
  38. int main (int argc, char *argv[]) {
  39. printf ("Entering Barrier test\n");
  40. printf ("Creating the barrier\n");
  41. Pthread_barrier_init (&barrier, NULL, 3);
  42. printf ("Creating the Threads\n");
  43. Pthread_create (null, NULL, &SOLDIER1, NULL);
  44. Pthread_create (null, NULL, &SOLDIER2, NULL);
  45. Pthread_create (null, NULL, &SOLDIER3, NULL);
  46. int i = 0;
  47. For (i = 0; i < i++) {
  48. printf ("timer in main thread:%d\n", I);
  49. Delay (200);
  50. }
  51. printf ("End of Main thread\n");
  52. Pthread_barrier_destroy (&barrier);
  53. return exit_success;
  54. }

http://blog.csdn.net/keyboardota/article/details/6867346

QNX multi-threaded synchronization barrier (barrier)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.