与c ++相关的pthread问题 [英] pthread problem related to c++
问题描述
以下pthread代码无法编译,我想知道原因。可以
有人给我这方面的指示吗?
如果我更换以下代码行
status1 = pthread_create(& threadID1,NULL,read,NULL);
with:
status1 = pthread_create(& threadID1,NULL,readerWriter :: read,NULL);
它也无法编译,如何编译?
谢谢,
大卫
----------------无法编译,想知道为什么-------
#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;
返回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();
返回0;
}
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;
}
推荐答案
" david wolf" <易**** @ gmail.com>在消息中写道
news:11 ******************** @ f14g2000cwb.googlegrou ps.com ...
"david wolf" <yi****@gmail.com> wrote in message
news:11********************@f14g2000cwb.googlegrou ps.com...
以下的pthread代码无法编译,我想知道原因。可以
有人给我这方面的指示吗?
如果我更换以下代码行
status1 = pthread_create(& threadID1,NULL,read,NULL) ;
:status1 = pthread_create(& threadID1,NULL,readerWriter :: read,NULL);
它也无法编译,如何编译?
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?
见项目33.2
-Mike
http://www.parashift.com/c++-faq-lite/
See item 33.2
-Mike
谢谢Mike,你的建议非常有用。以下是修改后的
代码。如果我定义并初始化全局锁和条件变量。
它将起作用。但是,如果我将这些放入构造函数中,它将无法工作。(
当然我必须取消注释这些全局变量并将它们定义为
成员变量)
#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 = PTH READ_COND_INITIALIZER;
// notFull = PTHREAD_COND_INITIALIZER;
}
void * readerWriter :: read(void * ptr){
cout<<<" read letter"<< endl;
返回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);
返回NULL; < br $>
}
int main(){
readerWriter rw;
rw。 createThreads();
返回0;
}
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;
}
Mike可以有人告诉我为什么我把这些int o构造函数,它
将不起作用。(
当然我必须取消注释这些全局变量并将它们定义为
成员变量)< br $>
非常感谢,
David
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
这篇关于与c ++相关的pthread问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!