main.cpp File Reference
Example code that uses the MyThread Library.
More...
#include "myThread.h"
#include <unistd.h>
#include <fcntl.h>
Go to the source code of this file.
|
Functions |
void | delay (void) |
void | thread1 (void) |
void | thread2 (void) |
void | thread3 (void) |
void | thread4 (void) |
void | thread5 (void) |
int | main (int argc, char **argv) |
Detailed Description
This program creates 5 new threads (other than the main thread). Each thread simply writes a message on the standard output.
Thread 1 and 4 terminate using
myThread_exit(), but the others continue executing in an infinite loop.
Sample Output:
$ g++ -march=i386 -c
myThread.cpp
$ g++ -march=i386 -c
main.cpp
$ g++ -o example.out myThread.o main.o
$ ./example.out
This is the Main
Thread
This is the Main
Thread
This is the Main
Thread
This is the Main
Thread
This is
Thread 1
This is
Thread 1
This is
Thread 1
This is
Thread 1
This is
Thread 2
This is
Thread 2
This is
Thread 2
This is
Thread 2
This is
Thread 3
This is
Thread 3
This is
Thread 3
This is
Thread 3
This is
Thread 4
This is
Thread 4
This is
Thread 4
This is
Thread 4
This is
Thread 5
This is
Thread 5
This is
Thread 5
- Note:
- write() is used instead of std::cout, in order to make the writes atomic, and hence prevent race conditions.
Definition in file main.cpp.