CSCI 4630 program 2, your namewhere you write your name, not literally "your name". If you have an inquiry about the assignment, do not use this subject, since I will just file it away if you do.
After the threads have done enough operations, you should expect your shared data to become corrupted. Design your threads to detect the corruption, report it and stop. Only report a situation where the data has become inconsistent because the update was not done in a critical section.
Run your program. Time it to see how long it takes before the data becomes corrupted.
When you turn in your program, say in the body of the email message how long your program ran until the data was bad.
Use function pthread_create to create a new thread. Create a variable attr of type pthread_attr_t, and initialize it as follows. Statements
pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);Then create the thread as follows.
stat = pthread_create(&tid, &attr, func, NULL);where tid is a variable of type pthread_t (typically another name for unsigned int) and func is a function that the thread will run. The prototype for func should be
void* func(void*);although you can call the function anything that you like. This creates and starts the new thread, and puts its thread id into variable tid.
You will need to wait for the threads to terminate. (If you don't wait, and you let the main program terminate, the threads that it created will be destroyed prematurely.) Function pthread_join allows one thread to wait for another one. use
pthread_join(tid,NULL);to wait for thread number tid to terminate before continuing. Since you will have created two threads, you will have to wait for both of them. You will have to wait for them in a specific order. It does not matter which one finishes first. A terminated thread waits until another thread joins with it.
There are several options for creating thread. They are described in the manual page for pthread_create. The options that have been given here (in the creation of the attr structure) will work for your purposes. Read the manual page for others.
gcc myprog.c -lpthreadWhen using threads, it is a good idea to include directive
#define _REENTRANTto force the compiler to generate reentrant code. (Most compilers do this anyway.) Reentrant code has a read-only TEXT segment (where the program is stored) so that it can be run simultaneously by more than one thread.
time_t tloc; time(&tloc); printf("%s", ctime(&tloc));Include header file time.h to use these. You can also use the POSIX function ctime_r, which is just a little more complex. See the manual page.