More Twin Primes

Modify your previous program so that when a program is found, it stores the first number in an twin prime in a list L1 and the second number in another list L2. (Just use STL lists.)

After the for loop, in a non-parallel region, print out the list of twin primes you found. Your output might look like:

Yes, there are twin primes between 100000 and 200000. 100151, 100153 100391, 100393 100517, 100519 100799, 100801 101111, 101113 101117, 101119 101159, 101161 100361, 100363 100547, 100549 . . . 108959, 113719 113717, 109201 109199, 113779 113777, 109519 109517, 114043 114041, 109453 109451, 109663 . . .

If you peruse the output, you might come across some numbers that are not paired correctly. In the sample output above, 113717 and 113719 are in L1 and L2 respectively, but the are not in the same positions. This is because several threads were attempting to write in L1 and L2. Your program might even crash.

You must guarantee that when a thread inserts a number in L1, no other thread is trying to modify L2. This is easily handled by designating some lines of code as "critical". Here is one way:

#pragma omp critical { // inset critical code here }

Modify your previous program by designating some sections of code "critical". Run your program a few times just to be safe.