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::r ead, 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::c reateThreads(){
pthread_t threadID1;
int status1;
//status1 = pthread_create( &threadID1, NULL, readerWriter::r ead,
NULL);
status1 = pthread_create( &threadID1, NULL, read, NULL);
status1 = pthread_join(th readID1, NULL);
}
int main(){
readerWriter rw;
rw.createThread s();
return 0;
} 10 4971
"david wolf" <yi****@gmail.c om> wrote in message
news:11******** ************@f1 4g2000cwb.googl egroups.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::r ead, NULL);
It cannot compile either, how to make it to be compiled? http://www.parashift.com/c++-faq-lite/
See item 33.2
-Mike
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_I NITIALIZER;
pthread_cond_t notEmpty = PTHREAD_COND_IN ITIALIZER;
pthread_cond_t notFull = PTHREAD_COND_IN ITIALIZER;
class readerWriter{
public:
void *read(void *ptr);
void createThreads() ;
readerWriter();
private:
pthread_mutex_t wlock;
pthread_cond_t notEmpty;
pthread_cond_t notFull;
};
readerWriter::r eaderWriter(){
//wlock = PTHREAD_MUTEX_I NITIALIZER;
//notEmpty = PTHREAD_COND_IN ITIALIZER;
//notFull = PTHREAD_COND_IN ITIALIZER;
}
void *readerWriter:: read(void *ptr){
cout<<"read letter "<<endl;
return NULL;
}
void readerWriter::c reateThreads(){
pthread_t threadID1;
int status1;
//status1 = pthread_create( &threadID1, NULL, readerWriter::r ead,
NULL);
status1 = pthread_create( &threadID1, NULL, readg, this);
status1 = pthread_join(th readID1, NULL);
}
void* readg(void *ptr){
readerWriter *tmprw = (readerWriter *)ptr;
tmprw->read(NULL);
return NULL;
}
int main(){
readerWriter rw;
rw.createThread s();
return 0;
}
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
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.
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::r eaderWriter(){
wlock = PTHREAD_MUTEX_I NITIALIZER;
notEmpty = PTHREAD_COND_IN ITIALIZER;
notFull = PTHREAD_COND_IN ITIALIZER;
}
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::r eaderWriter(){ wlock = PTHREAD_MUTEX_I NITIALIZER; notEmpty = PTHREAD_COND_IN ITIALIZER; notFull = PTHREAD_COND_IN ITIALIZER;
}
You're completely OT here, but the answer is that if you look in
pthread.h, you'll find that PTHREAD_{COND,M UTEX}_INITIALIZ ER is defined
as a struct initializer, i.e.
#define PTHREAD_MUTEX_I NITIALIZER { /* something here }
#define PTHREAD_COND_IN ITIALIZER { /* something here */ }
typedef some_struct pthread_mutex_t ;
typedef some_other_stru ct pthread_cond_t;
So the declarations:
pthread_mutex_t my_mutex = PTHREAD_MUTEX_I NITIALIZER;
pthread_cond_t my_cond = PTHREAD_COND_IN ITIALIZER;
are valid, while
pthread_mutex_t my_mutex;
pthread_cond_t my_cond;
my_mutex = PTHREAD_MUTEX_I NITIALIZER;
my_cond = PTHREAD_COND_IN ITIALIZER;
are not valid.
Thank you so much, Red Floyd. It's very helpful.
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
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_i nitialize(). This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 apilcation is running fine
except for the sleep cycle.
My sleep cylce is sleep(5) which is halting all the threads created from
execution. What i need here is the main thread creating the pthread should
only sleep but not all the threads created.
Is...
|
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 doing. I have copied and pasted
my other post below.
*****
I'm just trying to compile some older code that used the qt libraries
but had a make file manually written for it and was being used on an
|
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>
#include <sys/types.h>
|
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 are as following
|
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 order to solve the folowing problem:
We´re working on a server side application that implements threads
(pthread under linux Red Hat ES 2.1 kernel 2.4.9). The problem seems
| |
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 to know when it completes. The
time it takes for the thread to complete is variable and I don't want
to have to poll the thread for completion. I would like to avoid having
to use condition variables, so I thought setting up a signal handler to...
|
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()
- pthread_cond_broadcast()
My boss says we cant use Pthread because of licensing issues (LGPL) .. we
can only use Native Win32 threading/mutex functions.
|
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 leak would
be the most obvious cause but this appears to be more sinister then a
simple memory leak.
After alot of running around and searching through the code I found an
anomaly that I'd like to explain and understand if it's the cause of...
|
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 options but couldn't find any options for it.
Python support lthread? and how can I build python with it?
Thanks in advance,
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |