thread_util.cpp 770 Bytes
Newer Older
Sam Moore's avatar
Sam Moore committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "thread_util.h"

#include <sstream>
#include <string>

using namespace std;

pthread_mutex_t GetterThread::mutex = PTHREAD_MUTEX_INITIALIZER;

void * GetterThread::GetMessage(void * p)
{
	
	GetterThread * getter = (GetterThread*)(p);
	
	stringstream inputStream;

	char s = fgetc(getter->stream);
	while (s != '\n' && s != EOF)
	{
		
		inputStream << s;
		s = fgetc(getter->stream);
	}
	if (s == EOF)
	{
		getter->buffer = "";
		getter->buffer += s;
		return NULL;
	}	

	pthread_mutex_lock(&mutex);
		getter->buffer = inputStream.str();
	pthread_mutex_unlock(&mutex);
	
	getter->finished = true;

	return NULL;
}

void * TimerThread::Timeout(void * p)
{
	TimerThread * timer = (TimerThread*)(p);
	usleep(timer->count);
	timer->finished = true;
	return NULL;
}