Created
December 24, 2011 12:24
-
-
Save NightBrownie/1517245 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*#include "stdafx.h" | |
#include "Server.h" | |
#include <conio.h> | |
#include <iostream> | |
using namespace std; | |
Server* s; | |
class Listener : public ServerListener { | |
public: | |
void OnNewClient(int id) { | |
cout << "New client " << id << "\n"; | |
s->sendData(id, "Hello\n"); | |
} | |
void OnNewData(int id, char* data) { | |
cout << "<< [" << id << "] " << data << "\n"; | |
} | |
}; | |
void _tmain(void) | |
{ | |
WSADATA wsaData; | |
WORD version; | |
version = MAKEWORD( 2, 0 ); | |
WSAStartup( version, &wsaData ); | |
Listener* l = new Listener(); | |
pthread_t* thread1 = new pthread_t(); | |
s = new Server(1234, l, "One server"); | |
cout << "Hello from server" << s->name << endl; | |
pthread_create(thread1, NULL, Server::serve, s); | |
while(1); | |
return ; | |
}*/ | |
#include "stdafx.h" | |
#include <cstdlib> | |
#include <iostream> | |
#include <memory> | |
#include <pthread.h> | |
#include <Windows.h> | |
class Thread | |
{ | |
private: | |
pthread_t thread; | |
Thread(const Thread& copy); // copy constructor denied | |
public: | |
Thread() {} | |
virtual ~Thread() {} | |
static void* thread_func(void *d) { ((Thread *)d)->run(); return NULL;} | |
virtual void run() = 0; | |
int start() { return pthread_create(&thread, NULL, | |
Thread::thread_func, (void*)this); } | |
int wait () { return pthread_join (thread, NULL); } | |
}; | |
typedef std::auto_ptr<Thread> ThreadPtr; | |
int main(void) | |
{ | |
class Thread_a:public Thread | |
{ | |
public: | |
void run() | |
{ | |
for (int i=0; i<20; i++, Sleep(1)) | |
std::cout << "a " << std::endl; | |
} | |
}; | |
class Thread_b:public Thread | |
{ | |
public: | |
void run() | |
{ | |
for(int i=0; i<20; i++, Sleep(1)) | |
std::cout << " b" << std::endl; | |
} | |
}; | |
ThreadPtr a( new Thread_a() ); | |
ThreadPtr b( new Thread_b() ); | |
if (a->start() != 0 || b->start() != 0) | |
return EXIT_FAILURE; | |
if (a->wait() != 0 || b->wait() != 0) | |
return EXIT_FAILURE; | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment