Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.

As with most things, C++ has all of the same paralleling abilities as C as well as some other options:

The pthread package allows the user to explicitly make multi-threaded programs in C++ (as well as C).  

p_thread create takes a pthread object and a worker function: the thread will execute that worker function in parallel with the other threads that are in the program.  The pthread_join function waits until a thread is finished then joins it back to the thread of the main program, which ensures that there are not threads active for no reason in the program (there are only a finite number of threads that can be used in a program).  The Mutex_lock package gives the option for the programmer to deal with race conditions: if threads are accessing and modifying shared data in an unpredictable way, the critical section of the program that contains the shared data can be locked so that only one thread can access and modify it at a time.  This ensures that the parallelism reaches the same output each time it is called (unless there is explicit randomness in the program. 

The PASL library gives the programmer the option to use a fork-join technique much like in Ruby.  Then function fork2 spawns two threads, when then operate parallelly until they are done and join together. 

To deal with race conditions, the programmer is given the atomic class that provide flags that can be used similar to a semaphore: they are marked true when shared data is being used by some thread and then marked false when that thread is finished so some other thread can begin.  

The atomic class also provides built in functions that access, modify and write to a variable in a single step rather than three separate steps.  For example, to add 1 to a variable, 


can be used to access, modify and write a+1 to the variable a. 

Thanks to: