Create your own threads
Pthread basic example of pthread_create pthread_join.
Compile this with : g++ main.cpp -lpthread -lrt .
#include "stdint.h" #include "stdlib.h" #include "math.h" #include "iostream" #include "time.h" #include "pthread.h" void computePi() { double S = 0; for (int i = 1; i < 50000000; i++) { double I = i; S = S + 1 / (I*I); } std::cout.precision(20); std::cout << "S " << S << std::endl; std::cout << "Pi " << sqrt(S * 6) << std::endl; } void * thread_function(void *ptr) { int thread_id = (*(int *) ptr); std::cout << "start" << thread_id << std::endl; computePi(); } void start_thread(int nb) { pthread_t * threads = new pthread_t[nb]; int * id = new int[nb]; for (int i = 0; i < nb; i++) { id[i] = 1000 + i; pthread_create(&threads[i], NULL, (void* (*)(void*)) (&thread_function), (void *) &id[i]); } for (int i = 0; i < nb; i++) { pthread_join(threads[i], NULL); } delete threads; delete id; } int sub_main(int nb_thead) { timespec starting_time; timespec endding_time; clock_gettime(CLOCK_REALTIME, &starting_time); start_thread(nb_thead); clock_gettime(CLOCK_REALTIME, &endding_time); int64_t t1 = ((int64_t) starting_time.tv_sec)*1000000000 + starting_time.tv_nsec; int64_t t2 = ((int64_t) endding_time.tv_sec)*1000000000 + endding_time.tv_nsec; int64_t diff_time = t2 - t1; std::cout << "cpu time used " << diff_time << std::endl; } int main(int argc, char *argv[]) { int nb_thead; if (argc==2) nb_thead = atoi( argv[1] ); else nb_thead=1; std::cout << "start" << std::endl; sub_main(nb_thead); std::cout << "end" << std::endl; }
A more detailed Example based on Thread class
This example program illustrates the use of mutex variables with pthread_mutex_lock and pthread_mutex_unlock to avoid race conditions
#include <cstdlib> #include "pthread.h" #include <iostream> #include <vector> #include "math.h" using namespace std; class Thread { pthread_t threads; bool is_started; public: Thread() { } bool isStarted() const { return is_started; } static int runThread(void* arg) { return ((Thread*) arg)->run(); } int join() { if (is_started) return pthread_join(threads, NULL); else return 0; } int start() { int result = pthread_create(&threads, NULL, (void* (*)(void*)) runThread, this); is_started = true; return result; } virtual int run() { return 0; } }; class PiThread : public Thread { static double S; static bool init; static pthread_mutex_t mutexsum; unsigned int range_start; unsigned int range_end; public: PiThread(unsigned int rangestart,unsigned int rangeend) { this->range_start = rangestart; this->range_end = rangeend; initMutex(); } void static initMutex() { if (init == false) { init = true; pthread_mutex_init(&mutexsum, NULL); } } void static printResult() { std::cout.precision(20); std::cout << "S " << S << std::endl; std::cout << "Pi " << sqrt(S * 6) << std::endl; } void static AddResult(double partialSum) { pthread_mutex_lock(&mutexsum); std::cout.precision(20); std::cout << "partialSum " << partialSum << std::endl; PiThread::S += partialSum; pthread_mutex_unlock(&mutexsum); } // sum k= 1 to infinity of 1/k^2 = PI ^2/6 void computePi(unsigned int start,unsigned int end) { double partialSum = 0; for (int i = start; i < end; i++) { double I = i; partialSum = partialSum + 1 / (I * I); } AddResult(partialSum); } int run() { computePi(range_start, range_end); } }; bool PiThread::init = false; double PiThread::S = 0; pthread_mutex_t PiThread::mutexsum; int test_thread() { std::cout << ">> test_thread " << std::endl; std::vector<Thread*> *list = new std::vector<Thread*>(); const int range = 1000000000; for (unsigned int i = 0; i < 4; i++) { unsigned int start = 1 + i*range; unsigned int end = start + (range - 1); PiThread * t = new PiThread(start, end); list->push_back(t); t->start(); } for (int i = 0; i < list->size(); i++) { list->at(i)->join(); } PiThread::printResult(); std::cout << "<< test_thread " << std::endl; return 0; }