473,395 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

pthread problem related to c++

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;
}

Oct 7 '05 #1
10 4932

"david wolf" <yi****@gmail.com> wrote in message
news:11********************@f14g2000cwb.googlegrou ps.com...
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
Oct 7 '05 #2
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;
}

Oct 10 '05 #3
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

Oct 10 '05 #4
david wolf wrote:
Can Mike or someone tell me why if I put these into constructor, it
will not work.

Put what in?

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Oct 10 '05 #5
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;
}

Oct 12 '05 #6
david wolf wrote:
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.

Oct 12 '05 #7
Thank you so much, Red Floyd. It's very helpful.

David Wolf

Oct 17 '05 #8
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

Oct 17 '05 #9
david wolf wrote:
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().
Oct 17 '05 #10
Thanks a lot, red floyd. your suggestion is really helpful !!!

Oct 18 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: lokb | last post by:
Hi, I am creating a detach thread as shown below and caling pthread_create in a while loop where the file names in the directory are fetched and is passed as a parmater to pthread create. The...
1
by: Lionel van den Berg | last post by:
Hi all, I'm having a problem porting from qt2.x to qt3.x. I have posted on a qt forum but no joy as yet so I thought that maybe someone here could shed some more light on what the compiler is...
9
by: nan.li.g | last post by:
Hello, all, I have an interesting problem about stl map and pthread on Linux and g++. The source code is as follows. //mt_map_test.cpp #include <string> #include <map> #include <unistd.h>...
0
by: szehau | last post by:
Hi all, I have a program written in C with embeded SQL. Following are the configuration: DB2/LINUX 8.1.5 Thread model: posix gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7) My problems...
2
by: fran | last post by:
Server: IBM XSERIES 225 (Intel Xeon CPU 2.40GHz 1GB RAM) Operating System: Linux RedHat ES 2.1 kernel 2.4.9 Languaje: C++ Compiler: gcc 2.96 Libs: pthread We are in need of your help in...
2
by: chuckles | last post by:
Hullo all, as the subject says Does pthread emit a sigchld signal on successful exit? I have an app which starts a thread in response to a button press in a GUI. I can't join the thread but I need...
10
by: noleander | last post by:
I've got an application that uses Pthread to do threading. Mostly Im using Condition Variables and the associated function calls: - pthread_cond_wait() - pthread_cond_signal() -...
7
by: Louis B. (ldb) | last post by:
I have a long running program that eventually crashes when valloc() returns a 0. This program is relatively non-trivial as it's written in Ada, is multithreaded, has alot of SSE routines. A memory...
1
by: Akira Kitada | last post by:
Hi, I'm running Python 2.5 on FreeBSD 4. pthread on FreeBSD 4 has some problems so I would like to build python with lthread (linuxthreads) instead of BSD's pthread. So I looked at configure...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.