[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: OT: Pthread and GUI freeze
From:       Matt Rogers <matt () matt ! rogers ! name>
Date:       2003-11-04 2:42:42
[Download RAW message or body]

Hi,

On Monday 03 November 2003 07:52 am, Giuseppe Torelli wrote:
> Acc, after sending my email I found the problem !
> It was the endless for loop !
>
> I greatly apologize for disturbing you guys,
> Giuseppe Torelli
>
> Alle 14:50, luned́ 3 novembre 2003, hai scritto:
> > Hi,
> >
> > please forgive this small OT.
> > I'm always try to develop my first
> > KDE application. I create a thread that
> > opens the port 80. If the user is not root and the bind call
> > fails I emit an error message with KMessageBox.
> > Then I call pthread_exit(0) to close the pthread just created.
> >
> > The problem is that the GUI freezes and I can't close the messagge
> > box who appears ! This is the code:
> >
> > mizio.cpp:
> >
> > void Mizio::Check_Proxies()
> > {
> > 	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;			// this way pThis is now the equivalent of
> > this (pointer to Mizio class)
> > 									//pThis->CallAnotherFunction();
> > 	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 )
> >   {
> >   KMessageBox::error( pThis->Mizio_View, "Can't open port 80, maybe
> > another program is using it or you are not root !");
> >   pthread_exit( 0 );
> >   }
> >
> >   for( ;; )
> >   {
> >     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 );
> > }
> >   }
> >
> > What is wrong ? Could you please give me some directions where to fix the
> > problem ? Thank you so  much. I hope soon to release Mizio.
> >
> > --
> > Giuseppe Torelli

If you want to use threads, you might want to take a look at the QThread class 
rather than relying on the system having pthreads around because QThread will 
give you platform-independent threads. You can take a look at the API docs 
for QThread at http://doc.trolltech.com/3.1/qthread.html

Hope this helps,
Matt
-- 
"Ein says it's him, but Faye just yawns and says 'oh, please'"
        --Ed (Cowboy Bebop)
 
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic