J
John David Ratliff
Maybe someone here can help me. I want to use C++, but pthreads seems to be nonfunctional in it.
The following test code I've written in C and C++. The C version works, while the C++ one
does not. Can anyone tell me why?
--- main.c -> compile with gcc main.c -lpthread ---
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
static int p = 0;
static pthread_mutex_t mutex;
static pthread_cond_t cond;
static void *run(void *arg) {
printf("starting run...\n");
for (;
{
pthread_mutex_lock(&mutex);
while (p == 0) {
printf("nothing to do... taking a nap...\n");
pthread_cond_wait(&cond, &mutex);
printf("getting up...\n");
}
printf("p is now %d\n", p--);
pthread_mutex_unlock(&mutex);
}
}
static void play() {
for (;
{
printf("sleeping for 3 seconds...\n");
sleep(3);
printf("playing with the value of p...\n");
pthread_mutex_lock(&mutex);
++p;
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&cond);
}
}
int main(int argc, char **argv) {
pthread_t thread;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
if (pthread_create(&thread, NULL, run, NULL) != 0) {
perror("pthread_create");
exit(EXIT_FAILURE);
}
play();
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
--- end of main.c ---
Here is the C++ version that does not work...
--- main.cc - compile with g++ main.cc -lpthread ---
#include <cstdio>
#include <iostream>
#include <unistd.h>
#include <pthread.h>
using namespace std;
class ThreadTest {
int p;
pthread_mutex_t mutex;
pthread_cond_t cond;
public:
ThreadTest();
~ThreadTest();
void play();
void report_for_work();
};
static void *thread_run(void *arg) {
cout << "starting thread_run..." << endl;
ThreadTest obj = *(ThreadTest*)arg;
obj.report_for_work();
cout << "ending thread_run..." << endl;
return NULL;
}
ThreadTest::ThreadTest() {
pthread_t thread;
p = 0;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
if (pthread_create(&thread, NULL, thread_run, this) != 0) {
perror("pthread_create");
exit(EXIT_FAILURE);
}
}
ThreadTest::~ThreadTest() {
pthread_cond_destroy(&cond);
pthread_mutex_destroy(&mutex);
}
void ThreadTest:
lay() {
cout << "entering play..." << endl;
for (;
{
cout << "sleeping for 3 seconds..." << endl;
sleep(3);
cout << "incrementing p" << endl;
pthread_mutex_lock(&mutex);
++p;
pthread_mutex_unlock(&mutex);
cout << "calling up the worker..." << endl;
pthread_cond_signal(&cond);
}
cout << "leaving play..." << endl;
}
void ThreadTest::report_for_work() {
cout << "reporting for work..." << endl;
for (;
{
pthread_mutex_lock(&mutex);
while (p == 0) {
cout << "nothing to do. taking a nap..." << endl;
pthread_cond_wait(&cond, &mutex);
cout << "getting up..." << endl;
}
cout << "p is " << p-- << "..." << endl;
}
cout << "I've been called home..." << endl;
}
int main(int argc, char **argv) {
ThreadTest tt;
tt.play();
}
--- end of main.cc ---
They both compile, and both of them start threads. However, after the C++ thread goes to sleep,
it never wakes up, no matter how many times it is signaled.
I am using RedHat Fedora Linux, gcc/g++ 3.3.2
Please let me know if you have any ideas. Thanks,
--Dominic
Tired of bad newsreaders?
Get NaN! It's free and multi-platform!
http://www.technoplaza.net/nan/
The following test code I've written in C and C++. The C version works, while the C++ one
does not. Can anyone tell me why?
--- main.c -> compile with gcc main.c -lpthread ---
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
static int p = 0;
static pthread_mutex_t mutex;
static pthread_cond_t cond;
static void *run(void *arg) {
printf("starting run...\n");
for (;
pthread_mutex_lock(&mutex);
while (p == 0) {
printf("nothing to do... taking a nap...\n");
pthread_cond_wait(&cond, &mutex);
printf("getting up...\n");
}
printf("p is now %d\n", p--);
pthread_mutex_unlock(&mutex);
}
}
static void play() {
for (;
printf("sleeping for 3 seconds...\n");
sleep(3);
printf("playing with the value of p...\n");
pthread_mutex_lock(&mutex);
++p;
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&cond);
}
}
int main(int argc, char **argv) {
pthread_t thread;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
if (pthread_create(&thread, NULL, run, NULL) != 0) {
perror("pthread_create");
exit(EXIT_FAILURE);
}
play();
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
--- end of main.c ---
Here is the C++ version that does not work...
--- main.cc - compile with g++ main.cc -lpthread ---
#include <cstdio>
#include <iostream>
#include <unistd.h>
#include <pthread.h>
using namespace std;
class ThreadTest {
int p;
pthread_mutex_t mutex;
pthread_cond_t cond;
public:
ThreadTest();
~ThreadTest();
void play();
void report_for_work();
};
static void *thread_run(void *arg) {
cout << "starting thread_run..." << endl;
ThreadTest obj = *(ThreadTest*)arg;
obj.report_for_work();
cout << "ending thread_run..." << endl;
return NULL;
}
ThreadTest::ThreadTest() {
pthread_t thread;
p = 0;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
if (pthread_create(&thread, NULL, thread_run, this) != 0) {
perror("pthread_create");
exit(EXIT_FAILURE);
}
}
ThreadTest::~ThreadTest() {
pthread_cond_destroy(&cond);
pthread_mutex_destroy(&mutex);
}
void ThreadTest:
cout << "entering play..." << endl;
for (;
cout << "sleeping for 3 seconds..." << endl;
sleep(3);
cout << "incrementing p" << endl;
pthread_mutex_lock(&mutex);
++p;
pthread_mutex_unlock(&mutex);
cout << "calling up the worker..." << endl;
pthread_cond_signal(&cond);
}
cout << "leaving play..." << endl;
}
void ThreadTest::report_for_work() {
cout << "reporting for work..." << endl;
for (;
pthread_mutex_lock(&mutex);
while (p == 0) {
cout << "nothing to do. taking a nap..." << endl;
pthread_cond_wait(&cond, &mutex);
cout << "getting up..." << endl;
}
cout << "p is " << p-- << "..." << endl;
}
cout << "I've been called home..." << endl;
}
int main(int argc, char **argv) {
ThreadTest tt;
tt.play();
}
--- end of main.cc ---
They both compile, and both of them start threads. However, after the C++ thread goes to sleep,
it never wakes up, no matter how many times it is signaled.
I am using RedHat Fedora Linux, gcc/g++ 3.3.2
Please let me know if you have any ideas. Thanks,
--Dominic
Tired of bad newsreaders?
Get NaN! It's free and multi-platform!
http://www.technoplaza.net/nan/