Please complete the required studio exercises listed below, along with any of the (optional) enrichment exercises that interest you.
As you work through the exercises, please record your answers in a file, and upon completion please upload a file with your answers to the assignment page for this studio in Canvas.
Please make sure that the name of each person who worked on these exercises is listed in the first answer, and that you number your answers so they are easy for us to match up with the appropriate exercise.
lock_free
).
In that directory, edit a new C++ source code file (which should be named
something like lock_free.cpp
) and in it
write a main function whose signature looks like the standard (i.e.,
portable between Linux and Windows) shell (on Windows, terminal window)
program main function
entry point for C++: int main (int, char * [])
.
Please also use the #define
precompiler
directive to define a manifest constant whose value is 0, and have the main
function simply return that constant to indicate successful completion of the
program.
Implement the basic lock free stack shown in Listing 7.2 of [Williams], declare an instance of it
parameterized with int
in your main function, call its push method a few times
with different integer values, and then have main return 0 to indicate success. Build and run your
program (note that you may need to add the -latomic
flag when you compile), and as the answer to this exercise please explain why the push method's while
loop does not need to repeat the statement just before it that loaded the value of the atomic head
pointer into the new node's next pointer (i.e, why does it only contain an invocation of the
compare_exchange_weak
method?).
Build and run your program and as the answer to this exercise please indicate whether you saw evidence of the threads' pushes and pops interleaving in interesting ways. For example, did either of the containers end up with both even and odd numbers? Please describe what you saw and what it means for how the threads accesses to the lock free stack were interleaved.
Build and run your program, and as the answer to this exercise describe whether you saw any differences in the program's behavior compared to in the previous exercise, and if so what they were.
Build and run your program, and as the answer to this exercise describe whether you saw any differences in the program's behavior compared to in the previous exercise, and why you think there were or were not.
Build and run your program and as the answer to this exercise describe what you implemented and what results you saw (and any interesting behaviors you noticed) when you did that.