晚上在线等编程问题?? Cannot open include file: 'pthread.h': No such file or directory
大侠,你好,能问下这是什么原因吗?Cannotopenincludefile:'pthread.h':Nosuchfileordirectory绿竹子20:36:17我同...
大侠,你好,能问下这是什么原因吗? Cannot open include file: 'pthread.h': No such file or directory
绿竹子 20:36:17
我同学的c语言,我拿来应付老师,可是有1个错误哦,能麻烦下你吗 谢谢了
五、附录(代码):
Server.cpp:
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "time.h"
using namespace std ;
const int PORT = 5000 ;
queue<string> msgs ;
pthread_mutex_t mutex ;
vector<int> socks ;
int err_exit( const char *msg )
{
cout << msg << endl ;
return -1 ;
}
void* sendThread( void *arg )
{
time_t current_time;
char *timep;
string addtime;
while(1)
{
pthread_mutex_lock( &mutex ) ;
if( msgs.empty() )
{
pthread_mutex_unlock( &mutex ) ;
usleep( 500 ) ;
}
else
{
time(¤t_time);
timep=ctime(¤t_time);
addtime=timep;
msgs.front()=msgs.front()+"\n"+addtime;
while( !msgs.empty() )
{
for( vector<int>::iterator iter = socks.begin() ; iter != socks.end() ; ++iter )
send( *iter , msgs.front().c_str() , msgs.front().size()+1 ,0 ) ;
msgs.pop() ;
}
pthread_mutex_unlock( &mutex ) ;
}
}
return (void*)0 ;
}
void* clientThread( void *arg )
{
int sock = *(int*)arg ;
char buffer[1024] ;
while(1)
{
int byte = recv( sock , buffer , 1023 , 0 ) ;
if( byte <= 0 )
{
close( sock ) ;
pthread_exit( NULL ) ;
}
buffer[byte] = '\0' ;
pthread_mutex_lock( &mutex ) ;
msgs.push( string( buffer ) ) ;
pthread_mutex_unlock( &mutex ) ;
}
return (void*)0 ;
}
int main()
{
struct sockaddr_in server ;
server.sin_family = AF_INET ;
server.sin_port = htons( PORT ) ;
server.sin_addr.s_addr = INADDR_ANY ;
int sockfd = socket( AF_INET , SOCK_STREAM , 0 ) ;
if( sockfd == -1 )
err_exit( "socket error" ) ;
int reuse = 1 ;
setsockopt( sockfd , SOL_SOCKET , SO_REUSEADDR , &reuse , sizeof(int) ) ;
if( bind( sockfd , (struct sockaddr*)&server , sizeof( server ) ) == -1 )
err_exit( "bind error" ) ;
if( listen( sockfd , 5 ) == -1 )
err_exit( "listen error" ) ;
if( pthread_mutex_init( &mutex , NULL ) != 0 )
err_exit( "init error" ) ;
pthread_t sendTid ;
pthread_create( &sendTid , NULL , sendThread , NULL ) ;
while(1)
{
struct sockaddr_in remote ;
socklen_t remoteLen = sizeof( remote ) ;
int client = accept( sockfd , (struct sockaddr*)&remote , &remoteLen ) ;
if( client < 0 )
err_exit( "accept error" ) ;
socks.push_back( client ) ;
pthread_t clientTid ;
pthread_create( &clientTid , NULL , clientThread , (void*)&client ) ;
}
pthread_mutex_destroy( &mutex ) ;
return 0 ;
} 展开
绿竹子 20:36:17
我同学的c语言,我拿来应付老师,可是有1个错误哦,能麻烦下你吗 谢谢了
五、附录(代码):
Server.cpp:
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "time.h"
using namespace std ;
const int PORT = 5000 ;
queue<string> msgs ;
pthread_mutex_t mutex ;
vector<int> socks ;
int err_exit( const char *msg )
{
cout << msg << endl ;
return -1 ;
}
void* sendThread( void *arg )
{
time_t current_time;
char *timep;
string addtime;
while(1)
{
pthread_mutex_lock( &mutex ) ;
if( msgs.empty() )
{
pthread_mutex_unlock( &mutex ) ;
usleep( 500 ) ;
}
else
{
time(¤t_time);
timep=ctime(¤t_time);
addtime=timep;
msgs.front()=msgs.front()+"\n"+addtime;
while( !msgs.empty() )
{
for( vector<int>::iterator iter = socks.begin() ; iter != socks.end() ; ++iter )
send( *iter , msgs.front().c_str() , msgs.front().size()+1 ,0 ) ;
msgs.pop() ;
}
pthread_mutex_unlock( &mutex ) ;
}
}
return (void*)0 ;
}
void* clientThread( void *arg )
{
int sock = *(int*)arg ;
char buffer[1024] ;
while(1)
{
int byte = recv( sock , buffer , 1023 , 0 ) ;
if( byte <= 0 )
{
close( sock ) ;
pthread_exit( NULL ) ;
}
buffer[byte] = '\0' ;
pthread_mutex_lock( &mutex ) ;
msgs.push( string( buffer ) ) ;
pthread_mutex_unlock( &mutex ) ;
}
return (void*)0 ;
}
int main()
{
struct sockaddr_in server ;
server.sin_family = AF_INET ;
server.sin_port = htons( PORT ) ;
server.sin_addr.s_addr = INADDR_ANY ;
int sockfd = socket( AF_INET , SOCK_STREAM , 0 ) ;
if( sockfd == -1 )
err_exit( "socket error" ) ;
int reuse = 1 ;
setsockopt( sockfd , SOL_SOCKET , SO_REUSEADDR , &reuse , sizeof(int) ) ;
if( bind( sockfd , (struct sockaddr*)&server , sizeof( server ) ) == -1 )
err_exit( "bind error" ) ;
if( listen( sockfd , 5 ) == -1 )
err_exit( "listen error" ) ;
if( pthread_mutex_init( &mutex , NULL ) != 0 )
err_exit( "init error" ) ;
pthread_t sendTid ;
pthread_create( &sendTid , NULL , sendThread , NULL ) ;
while(1)
{
struct sockaddr_in remote ;
socklen_t remoteLen = sizeof( remote ) ;
int client = accept( sockfd , (struct sockaddr*)&remote , &remoteLen ) ;
if( client < 0 )
err_exit( "accept error" ) ;
socks.push_back( client ) ;
pthread_t clientTid ;
pthread_create( &clientTid , NULL , clientThread , (void*)&client ) ;
}
pthread_mutex_destroy( &mutex ) ;
return 0 ;
} 展开
展开全部
不是讲的非常明白吗?你没有pthread.h那个文件,也就是你没有那个pthread的库,去网上找一个安上就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <pthread.h>
这个文件不在库文件夹里面。
这个文件不在库文件夹里面。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询