pthread problem related to c++

D

david wolf

The following pthread code cannot compile, I want to know why. Can
somebody give me direction on this?

if I replace the following line of code

status1 = pthread_create(&threadID1, NULL, read, NULL);

with:

status1 = pthread_create(&threadID1, NULL, readerWriter::read, NULL);

It cannot compile either, how to make it to be compiled?

Thanks,

David
----------------cannot compile, want to know why-------
#include <pthread.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

class readerWriter{
public:
void *read(void *ptr);
void createThreads();
};

void *readerWriter::read(void *ptr){

cout<<"read letter "<<endl;

return NULL;
}

void readerWriter::createThreads(){
pthread_t threadID1;
int status1;


//status1 = pthread_create(&threadID1, NULL, readerWriter::read,
NULL);
status1 = pthread_create(&threadID1, NULL, read, NULL);
status1 = pthread_join(threadID1, NULL);


}

int main(){
readerWriter rw;
rw.createThreads();

return 0;
}
 
M

Mike Wahler

david wolf said:
The following pthread code cannot compile, I want to know why. Can
somebody give me direction on this?

if I replace the following line of code

status1 = pthread_create(&threadID1, NULL, read, NULL);

with:

status1 = pthread_create(&threadID1, NULL, readerWriter::read, NULL);

It cannot compile either, how to make it to be compiled?

http://www.parashift.com/c++-faq-lite/
See item 33.2

-Mike
 
D

david wolf

Thanks Mike, you suggestion is very useful. Following is the modified
code. If i define and initialize global lock and conditional variables.
it will work. But if I put these into constructor, it will not work.(of
course I have to uncomment these global variables and define them as
member variables)


#include <pthread.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>



void* readg(void *ptr);

pthread_mutex_t wlock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t notEmpty = PTHREAD_COND_INITIALIZER;
pthread_cond_t notFull = PTHREAD_COND_INITIALIZER;



class readerWriter{
public:
void *read(void *ptr);
void createThreads();
readerWriter();
private:
pthread_mutex_t wlock;
pthread_cond_t notEmpty;
pthread_cond_t notFull;
};

readerWriter::readerWriter(){
//wlock = PTHREAD_MUTEX_INITIALIZER;
//notEmpty = PTHREAD_COND_INITIALIZER;
//notFull = PTHREAD_COND_INITIALIZER;

}

void *readerWriter::read(void *ptr){

cout<<"read letter "<<endl;

return NULL;
}

void readerWriter::createThreads(){
pthread_t threadID1;
int status1;


//status1 = pthread_create(&threadID1, NULL, readerWriter::read,
NULL);
status1 = pthread_create(&threadID1, NULL, readg, this);
status1 = pthread_join(threadID1, NULL);


}

void* readg(void *ptr){
readerWriter *tmprw = (readerWriter *)ptr;
tmprw->read(NULL);
return NULL;

}

int main(){
readerWriter rw;
rw.createThreads();

return 0;
}
 
D

david wolf

Can Mike or someone tell me why if I put these into constructor, it
will not work.(of
course I have to uncomment these global variables and define them as
member variables)

Thanks a lot,

David
 
D

david wolf

What I mean is that why, if I put these three lines of code in the
constructor, it does not work.
(of course, I need to declare these as member variables of the class)
readerWriter::readerWriter(){
wlock = PTHREAD_MUTEX_INITIALIZER;
notEmpty = PTHREAD_COND_INITIALIZER;
notFull = PTHREAD_COND_INITIALIZER;


}
 
R

red floyd

david said:
What I mean is that why, if I put these three lines of code in the
constructor, it does not work.
(of course, I need to declare these as member variables of the class)
readerWriter::readerWriter(){
wlock = PTHREAD_MUTEX_INITIALIZER;
notEmpty = PTHREAD_COND_INITIALIZER;
notFull = PTHREAD_COND_INITIALIZER;


}

You're completely OT here, but the answer is that if you look in
pthread.h, you'll find that PTHREAD_{COND,MUTEX}_INITIALIZER is defined
as a struct initializer, i.e.

#define PTHREAD_MUTEX_INITIALIZER { /* something here }
#define PTHREAD_COND_INITIALIZER { /* something here */ }

typedef some_struct pthread_mutex_t;
typedef some_other_struct pthread_cond_t;

So the declarations:

pthread_mutex_t my_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t my_cond = PTHREAD_COND_INITIALIZER;

are valid, while

pthread_mutex_t my_mutex;
pthread_cond_t my_cond;
my_mutex = PTHREAD_MUTEX_INITIALIZER;
my_cond = PTHREAD_COND_INITIALIZER;

are not valid.
 
D

david wolf

So I guess, there's no way in pthread to define a mutex as a member
variable of a class, is my understanding correct?

Can someone help on this question? I just wish to not poluate the
global space.

--David
 
R

red floyd

david said:
So I guess, there's no way in pthread to define a mutex as a member
variable of a class, is my understanding correct?

Can someone help on this question? I just wish to not poluate the
global space.

--David

Sure there is. Don't pass it an initializer, but in your class
constructor call pthread_mutex_initialize().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top