LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-31-2003, 04:02 AM   #1
gt73
Member
 
Registered: Sep 2003
Distribution: LDME
Posts: 43

Rep: Reputation: 15
pthread and C++


Hi,

I need some help to use pthreads in C++:

this is the code in mizio.cpp:

void Mizio::Check()
{
Mizio_View->Button_Check->setEnabled ( false );
pthread_t Mizio_Server_Thread;
pthread_create( &Mizio_Server_Thread, NULL, Mizio_Server, (void*)NULL );
QListViewItemIterator it( Mizio_View->mizio_listview );
while ( it.current() )
{
//do something
}

}

and this is mizio.h:

class Mizio : public KMainWindow
{
Q_OBJECT
public:
Mizio();
KAboutApplication *aboutMizio;
virtual ~Mizio();
void* Mizio_Server( void* );
void* Mizio_Client( void* socket );

protected:
class Mizio_CentralWidgetBase *Mizio_View;

private slots:
void ShowAboutWindow();
void Mizio_Exit();
void Retrieve();
void Check();
void Export();
};

I got these errors from g++:

mizio.cpp: In member function `void Mizio::Check_Proxies()':
* mizio.cpp:52: no matches converting function `Mizio_Server' to type ` void*(*)(void*)'
* mizio.h:37: candidates are: void* Mizio::Mizio_Server(void*)
* mizio.cpp: In member function `void* Mizio::Mizio_Server(void*)':
* mizio.cpp:118: no matches converting function `Mizio_Client' to type ` void*(*)(void*)'
* mizio.h:38: candidates are: void* Mizio::Mizio_Client(void*)

I red somewhere I have to use some wrappers but my knowledge of C++ is not so good, could someone help me so I understand the problem ?

Thank you so much,
 
Old 10-31-2003, 04:29 AM   #2
Skute
Member
 
Registered: Jul 2003
Distribution: SUSE 9.0 Pro
Posts: 45

Rep: Reputation: 15
i had similar trouble calling a member function as a new thread.

The new thread has to be static, so you can try declaring the function as static, but i dont think that will help.

Take function out of the class, make it similar to this:

void *ReadThread(void* pParam)
{
// Stuff for your thread goes in here!
MyClass* pThis = NULL;

pThis = (MyClass *)pParam;

// pThis is now the equivalent of this (pointer to your class)

pThis->CallAnotherFunction();
}

void MyClass::CreateThreadFunction()
{
// In here we will create the thread
iResult = pthread_create(&threadid, NULL, ReadThread, this
}


Hope that helps
 
Old 10-31-2003, 04:33 AM   #3
gt73
Member
 
Registered: Sep 2003
Distribution: LDME
Posts: 43

Original Poster
Rep: Reputation: 15
>The new thread has to be static, so you can try declaring the function as static, but i dont think that will help.

Thank you for your kind answer, so how did you solve your problem ? Around the web there are some libraries but I don't want to install a new library, just some hints on how to succed the compilation.

Thank you,
 
Old 10-31-2003, 04:50 AM   #4
Skute
Member
 
Registered: Jul 2003
Distribution: SUSE 9.0 Pro
Posts: 45

Rep: Reputation: 15
do as i did above, remove the function from your class and put it in above the function you are calling it from
 
Old 10-31-2003, 04:51 AM   #5
gt73
Member
 
Registered: Sep 2003
Distribution: LDME
Posts: 43

Original Poster
Rep: Reputation: 15
Thank you so much ! It worked without a warning !
 
Old 10-31-2003, 04:54 AM   #6
Skute
Member
 
Registered: Jul 2003
Distribution: SUSE 9.0 Pro
Posts: 45

Rep: Reputation: 15
glad to help
 
Old 11-03-2003, 08:34 AM   #7
gt73
Member
 
Registered: Sep 2003
Distribution: LDME
Posts: 43

Original Poster
Rep: Reputation: 15
Hi,

I need another small help. In one of the function that is pthreaded I need two arguments, the accepted socket descriptor and the QWidget pointer:

void Mizio::Check_()
{
Mizio_View->Button_Check->setEnabled ( false );
pthread_t Mizio_Server_Thread;
pthread_create( &Mizio_Server_Thread, NULL, Mizio_Server, this );
}

void* Mizio_Server ( void* dummy)
{
Mizio* pThis = NULL;
pThis = (Mizio *)dummy;
pthread_t Client_Thread;
int Server_Socket;
int accepted_socket;
int size;
struct sockaddr_in port_addr, accept_addr;
int set_opt = 1;
fd_set read_set;
int ready_fd;

/* set up the socket on the server port */
port_addr.sin_family = AF_INET;
port_addr.sin_addr.s_addr = htonl( INADDR_ANY );
port_addr.sin_port = htons( SERVER_PORT );

Server_Socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
setsockopt( Server_Socket, SOL_SOCKET, SO_REUSEADDR, (char*)&set_opt, sizeof( set_opt ) );
if ( bind(Server_Socket, (struct sockaddr*)&port_addr, sizeof( port_addr )) > 0 )
{
while(1)
{
listen( Server_Socket, 5 ); // wait for the client to try to connect
FD_ZERO( &read_set );
FD_SET( Server_Socket, &read_set );

do {
ready_fd = select( Server_Socket+1, &read_set, NULL, NULL, NULL );
}
while ( ready_fd <= 0 || !FD_ISSET( Server_Socket, &read_set ) );

// a client has connected. Accept their connection and create a thread to serve the client
size = sizeof( accept_addr );
accepted_socket = accept( Server_Socket, (struct sockaddr*)&accept_addr, &size );
pthread_create( &Client_Thread, NULL, Mizio_Client, (void*)accepted_socket );
}
}

else
{
KMessageBox::error( pThis->Mizio_View, "Can't open port 80, maybe another program is using it or you are not root !");
pthread_exit( 0 );
}

}

//So now I have to create a thread for each connected client. I need the same arguments in the Mizio_Server function plus the argument accepted_socket:

void* Mizio_Client ( void* socket )
{
char buffer[255];

while ( read( (int)socket, buffer,255 ) > 0)
{
printf ("%s",buffer);
}
close( (int)socket );
pthread_exit( NULL );
}

#include "mizio.moc"

How to have the QWidget (passed with 'this' in Mizio_Server) in this function ?
I red that I can use struct but the compiler keeps giving me syntax error when
I try to use struct_name.mizio_QWidget_pointer_name

Any idea ?? What if I send you the whole package .tar.gz by email ?
You will need QT, XFree and KDE headers to compile it.

Thank you,
 
Old 11-03-2003, 08:46 AM   #8
Skute
Member
 
Registered: Jul 2003
Distribution: SUSE 9.0 Pro
Posts: 45

Rep: Reputation: 15
you have to create a parameter struct and pass it through.

struct tagParam
{
int Param1;
int Param2;
int Param3;
}tParam, *pParam;

then just pass tParam / pParam through to createthread
 
Old 11-03-2003, 08:51 AM   #9
gt73
Member
 
Registered: Sep 2003
Distribution: LDME
Posts: 43

Original Poster
Rep: Reputation: 15
Ehm, which one shall I pass to the create thread function ? tParam or pParam ?
Sorry to bother you.
 
Old 11-03-2003, 09:01 AM   #10
Skute
Member
 
Registered: Jul 2003
Distribution: SUSE 9.0 Pro
Posts: 45

Rep: Reputation: 15
tparam is an actual object, pparam is a pointer.

so if you require a pointer, use the pointer, if you require the actual object, use the object.

with create thread i think you need the pointer.


or,

you can just send through the address of the object, eg:
&tParam

thats the same as

pParam

 
Old 11-03-2003, 09:03 AM   #11
gt73
Member
 
Registered: Sep 2003
Distribution: LDME
Posts: 43

Original Poster
Rep: Reputation: 15
Thank you very much Skute, I appreciate your kindness
 
Old 11-03-2003, 09:07 AM   #12
Skute
Member
 
Registered: Jul 2003
Distribution: SUSE 9.0 Pro
Posts: 45

Rep: Reputation: 15
no worries
 
Old 11-03-2003, 09:25 AM   #13
gt73
Member
 
Registered: Sep 2003
Distribution: LDME
Posts: 43

Original Poster
Rep: Reputation: 15
void* Mizio_Server ( void* dummy)
{
Mizio* pThis = NULL;
pThis = (Mizio *)dummy;
struct Mizio_structure
{
QWidget *pThis;
int accepted_socket;
} *str_Pointer;

.......

114: str_Pointer.accepted_socket = accept( Server_Socket, (struct sockaddr*)&accept_addr, &size );
115: pthread_create( &Client_Thread, NULL, Mizio_Client, (void*)str_Pointer.accepted_socket );

but when compiling:

mizio.cpp:114: request for member `accepted_socket' in `str_Pointer', which is of non-aggregate type `Mizio_Server(void*)::Mizio_structure*'

and:

mizio.cpp:115: request for member `accepted_socket' in `str_Pointer', which is of non-aggregate type `Mizio_Server(void*)::Mizio_structure*'

Where was I wrong ?
 
Old 11-03-2003, 09:28 AM   #14
Skute
Member
 
Registered: Jul 2003
Distribution: SUSE 9.0 Pro
Posts: 45

Rep: Reputation: 15
void*)str_Pointer.accepted_socket

you are sending just the socket over, surely you want to send the whole object over?
 
Old 11-03-2003, 09:32 AM   #15
gt73
Member
 
Registered: Sep 2003
Distribution: LDME
Posts: 43

Original Poster
Rep: Reputation: 15
You are right, I have to send the whole structure but I have to fill the variable accepted_socket first right ? So I can use it later, but why do I get the error message ? Also do you know a link or a book that explains those arabic error messages the compiler outputs ?

Thank you,
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pthread JanusPaul Programming 8 02-09-2005 05:46 PM
pthread zaman Programming 6 08-29-2004 04:04 PM
about pthread c12ayon Programming 6 10-25-2003 04:37 AM
need help about pthread c12ayon Programming 1 10-24-2003 07:47 AM
Pthread rch Programming 1 05-28-2003 02:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration