Error C4996 'strerror': This function or variable may be unsafe. Consider using strerror_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
Error C4996 'gethostbyname': Use getaddrinfo() or GetAddrInfoW() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings
Error C4996 'inet_ntoa': Use inet_ntop() or InetNtop() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings
But after I change the code to the below, its gone.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#ifdef _WIN32
#include <winsock.h> // For socket(), connect(), send(), and recv()
//below added by me
//#include <WinSock2.h> // For socket(), connect(), send(), and recv()
//#include <Ws2tcpip.h>
//#pragma comment(lib, "Ws2_32.lib")
typedefint socklen_t;
typedefchar raw_type;
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netinet/in.h>
typedefvoid raw_type;
#endif
Error C4996 'strerror': This function or variable may be unsafe. Consider using strerror_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
But i got the below error codes in my cout, as shown below. Any idea what it means? How to overcome this errors?
E0349 no operator "<<" matches these operands
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'pthread_t' (or there is no acceptable conversion)
1 2 3 4 5 6 7 8
catch (SocExcept& e) {
cerr << "Unable to get the port" << endl;
}
cout << " thread " << pthread_self() << endl; ///error occurs at this line.
Since issue seen only with pthread, I suspect in pthread.h, should below struct be changed? is it VS2019 compiler bug in windows? is there any other way to get the thread id of a pthread in c++ windows?
1 2 3 4 5 6 7 8 9 10 11 12
/*
* Generic handle type - intended to extend uniqueness beyond
* that available with a simple pointer. It should scale for either
* IA-32 or IA-64.
*/
typedefstruct {
void * p; /* Pointer to actual object */
unsignedint x; /* Extra information - reuse count etc */
} ptw32_handle_t;
typedef ptw32_handle_t pthread_t;
Refactoring to use std::thread is a solution. But your error is that it doesn't know how to use the << operator with cout (std::ostream&) and a pthread_t.
If you got rid of that cout line, see if it compiles. Then, once you get it to compile, explain what you are actually trying to print out with that line.
Ok I have tried to revert those changes mentioned in the below link. But i got the below set of errors in VS2019. what is missing here now?
how can I use C++ threads? Do i need to go to NuGet Packages..>Browse>search and find pthreads >Install? OR use Vcpkg to install and use pthread in Windows? I know that pthread is for Linux POSIX threads and is not combined with VS2019.
Severity Code Description Project File Line Suppression State
Error (active) E1696 cannot open source file "pthread.h"
Error (active) E0020 identifier "pthread_t" is undefined
Error (active) E0020 identifier "pthread_create" is undefined
Error (active) E0020 identifier "pthread_self" is undefined
Error (active) E0020 identifier "pthread_detach" is undefined
Error (active) E0020 identifier "pthread_self" is undefined
After i installed pthread using NuGet, all those above errors are gone, but I got the below error, which was resolved by adding the below line in \packages\pthreads.2.9.1.4\build\native\include\pthread.h
below is my code snippet.
I still see the below errors. What could be wrong?
E0349 no operator "<<" matches these operands
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'pthread_t' (or there is no acceptable conversion)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void TestClient(Socket* sock) {
cout << "Testing client ";
try {
cout << sock->getAddr() << ":";
}
catch (SocketExcept& e) {
cerr << "Unable to get address" << endl;
}
try {
cout << sock->getPort();
}
catch (SocketExcept& e) {
cerr << "Unable to get port" << endl;
}
cout << " with thread " << pthread_self() << endl; //ERROR occurs in this line.
pthread_t may be an integer, or it may be a pointer.
It's an opaque type you're not meant to know the internal representation of. You can store it, and compare it with other pthread_t values, but that's about it.
With this in mind, your best hope is
cout << " with thread " << reinterpret_cast<unsignedlonglong>(pthread_self()) << endl;
It's not a value you can do a lot with, except that it's "for information only".
The actual values likely won't make any sense, but if you create 5 threads, then you'll see that from having 5 different thread id's printed out.
Bear in mind if you're using these to track thread creation and destruction in your log files, that having destroyed a thread, you may (or may not) get that same thread-id back in a subsequent thread create.
how can I use C++ threads? Do i need to go to NuGet Packages..>Browse>search and find pthreads >Install? OR use Vcpkg to install and use pthread in Windows?
std::thread is part of the C++11 standrd so there is no need to install anything.
Another option to use might be std::async which is higher level than std::thread.
Why don't you start from the beginning and tell us what you actually want to do.
I am trying to create a client server stream echo based application using c++ and sockets for windows based. I had installed pthread using NuGet. do you want me to remove it?
After I remove pthread package, it shows the below errors.
How can I get rid of this?
Severity Code Description Project File Line Suppression State
Error (active) E1696 cannot open source file "pthread.h"
Error (active) E0020 identifier "pthread_t" is undefined
Error (active) E0020 identifier "pthread_create" is undefined
Error (active) E0020 identifier "pthread_self" is undefined
Error (active) E0020 identifier "pthread_detach" is undefined
Error (active) E0020 identifier "pthread_self" is undefined
While i tried to add the below line, it shows errors.
E0171 invalid type conversion
C2440 'reinterpret_cast':cannot convert from 'pthread_t to 'unsigned_int64'
What does this mean?
cout << " with thread " << reinterpret_cast<unsignedlonglong>(pthread_self()) << endl;
A number of articles on stackoverflow suggest that pthread_t is an opaque type in some implementations. That would prevent you from casting it to an integral type.