> As currently I am seeing that with above code, both thread are executing the function.
What did you expect?
If you wanted capture1 to run to completion, then capture2 to run to completion, then why are you even messing about with threads?
If you want this behaviour with threads, then you need a different approach.
Works for me
Code:
#include <iostream>
#include <pthread.h>
using namespace std;
pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
class Student
{
public:
Student() : last(0) { }
static void *capture1(void *arg) {
Student *cap = static_cast<Student*>(arg);
for ( int i = 0 ; i < 10000 ; i++ ) {
pthread_mutex_lock(&count_mutex);
cap->dowork(i);
pthread_mutex_unlock(&count_mutex);
}
return NULL;
}
static void *capture2(void *arg) {
Student *cap = static_cast<Student*>(arg);
for ( int i = 0 ; i < 10000 ; i++ ) {
pthread_mutex_lock(&count_mutex);
cap->dowork(i);
pthread_mutex_unlock(&count_mutex);
}
return NULL;
}
static void *capture3(void *arg) {
Student *cap = static_cast<Student*>(arg);
for ( int i = 0 ; i < 10000 ; i++ ) {
pthread_mutex_lock(&count_mutex);
cap->dowork(i);
pthread_mutex_unlock(&count_mutex);
}
return NULL;
}
static void *capture4(void *arg) {
Student *cap = static_cast<Student*>(arg);
for ( int i = 0 ; i < 10000 ; i++ ) {
pthread_mutex_lock(&count_mutex);
cap->dowork(i);
pthread_mutex_unlock(&count_mutex);
}
return NULL;
}
void dowork(int i) {
if ( last != pthread_self() ) {
cout << "Worker " << this
<< " on thread " << pthread_self()
<< " resumed at " << i << endl;
last = pthread_self();
}
}
void start() {
int rc;
pthread_mutex_lock(&count_mutex);
pthread_attr_t attr;
cout << "Creating threads....";
pthread_attr_init(&attr);
rc = pthread_create(&threads[0], &attr, capture1, (void *)this);
rc = pthread_create(&threads[1], &attr, capture2, (void *)this);
rc = pthread_create(&threads[2], &attr, capture3, (void *)this);
rc = pthread_create(&threads[3], &attr, capture4, (void *)this);
pthread_attr_destroy(&attr);
cout << "done" << endl;
pthread_mutex_unlock(&count_mutex);
}
void end() {
void *res;
pthread_join(threads[0],&res);
pthread_join(threads[1],&res);
pthread_join(threads[2],&res);
pthread_join(threads[3],&res);
}
private:
pthread_t threads[4];
pthread_t last;
};
int main ( ) {
Student s;
cout << "Main Start" << endl;
s.start(); // sow
s.end(); // reap
cout << "Main End" << endl;
}
$ g++ foo.cpp -pthread
$ ./a.out
Main Start
Creating threads....done
Worker 0x7fff037a2980 on thread 140706368927488 resumed at 0
Worker 0x7fff037a2980 on thread 140706377320192 resumed at 0
Worker 0x7fff037a2980 on thread 140706352142080 resumed at 0
Worker 0x7fff037a2980 on thread 140706368927488 resumed at 755
Worker 0x7fff037a2980 on thread 140706360534784 resumed at 0
Worker 0x7fff037a2980 on thread 140706368927488 resumed at 927
Worker 0x7fff037a2980 on thread 140706360534784 resumed at 142
Worker 0x7fff037a2980 on thread 140706352142080 resumed at 185
Worker 0x7fff037a2980 on thread 140706360534784 resumed at 143
Worker 0x7fff037a2980 on thread 140706352142080 resumed at 356
Worker 0x7fff037a2980 on thread 140706377320192 resumed at 210