Monday, March 2, 2015

Problems For Learning Multithreading.

  1. Write a function add(int, int) and spawn a new thread to call this function.
    Print the output on the console from the thread.
  2. Create a thread routine that adds all the numbers passed to it as argument and return the result to the main thread.
  3. Write a program having a global variable x, which is initialised with some
    value, say 10. Now create two threads and increment value of x from both the threads. Also, print the value of x inside thread routines and before main routine finishes. What do you observe? What is the behaviour when program is run multiple times?
  4. Write a program having a global variable x, which is initialised with some
    value, say 10. Now create two threads and increment value of x from both the threads. Protect the global variable x using a locking mechanism. Use mutex.
  5. Write a program having a global variable x, which is initialised with some
    value, say 10. Now create two threads and increment value of x from both the threads. Protect the global variable x using a locking mechanism. Use spinlock.
  6. Open a file and use 5 threads to write to the file. Increase elements in a
    loop in the threads in a loop and write the elements to the file. What is happening here. What should be the data in the file.
  7. Open a file and use 5 threads to write to the file. Increase elements in a
    loop in the threads in a loop and write the elements to the file. What is happening here. Make sure that the threads write to the file one by one - for example if the threads are A, B, C, D and E - write the program such that the content of the file is in the order ABCDEABCDE.
  8. Using multithreading add all the element of a array. Take a large array say,
    10^8 elements and divide the work across all the threads equally. Compare the time take by a single threaded program.
  9. Using multithreading sort a array in ascending order using Merge sort. Take a
    large array, say 1068 elements and compare the working with a single threaded program.
  10. Though you are writing multi threaded code - you might observe that some of
    your processor cores are not getting used, and some of your threads are sleeping. How will you - 1. Make sure that all the threads get maximum available resources on your desktop. 2. All the processor cores are busy all the time.
  11. What are thread safe functions? Write a function which has a static variable
    • and it increments it. Make it thread safe.
  12. What are re-entrant functions? Write a function which has a static variable
    • and it increments it. Make it re-entrant.
  13. What are atomic instructions - why are they needed, how do you have atomicity
    in the code. For example in an addition function which has a counter of “How many times it was called”.
  14. Open and read a file, keep writing some data using a thread to the file. Now
    whenever you get a keyboard interuppt (for example say ctrl+w) count the number of characters in the file and display it on the screen. This counting has to be done in a new thread. Similiary another thread should calculate the number of words and the number of vowels in the file.
  15. Write a multithreaded program where one thread is writing some data to a file,
    and on receiving some interrupt from keyboard (say, ctrl+w), perform following operations using separate threads and display the results on screen: a. Count number of characters written to the file b. Count number of words in the file c. Count number of vowels in the file
    While solving the above question you will observe that you have to take lock on the file. When you take lock - the write thread will be blocked. What do you suggest. How can we minimize the blocking of the write thread. Code them as well.
  16. Simulate the consumer - producer problem.
  17. Simulate a deadlock situation, the deadlock should be due to improper lock ordering.
  18. Implement a mutex library.
  19. Implement a spinlock library.


    Thanks Adheer Chandravanshi<adheerchandravanshi@gmail.com> for reviewing the questions.