Click here to Skip to main content
15,896,269 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionneed help with vector<student*>* students pointer Pin
neodeaths27-Aug-12 0:28
neodeaths27-Aug-12 0:28 
AnswerRe: need help with vector* students pointer Pin
Maximilien27-Aug-12 0:45
Maximilien27-Aug-12 0:45 
QuestionRe: need help with vector* students pointer Pin
David Crow27-Aug-12 2:34
David Crow27-Aug-12 2:34 
AnswerRe: need help with vector* students pointer Pin
Stefan_Lang27-Aug-12 23:30
Stefan_Lang27-Aug-12 23:30 
Questionfrom sync TCP/IP to async Pin
bkelly1326-Aug-12 15:42
bkelly1326-Aug-12 15:42 
AnswerRe: from sync TCP/IP to async Pin
pasztorpisti26-Aug-12 22:25
pasztorpisti26-Aug-12 22:25 
GeneralRe: from sync TCP/IP to async Pin
bkelly1327-Aug-12 2:41
bkelly1327-Aug-12 2:41 
GeneralRe: from sync TCP/IP to async Pin
pasztorpisti27-Aug-12 4:24
pasztorpisti27-Aug-12 4:24 
That's around 10MByte/sec, thats quite OK, with good network 100MByte/sec can be achieved. You might want to set the send/receive buffer size of your sockets (setsockopt/SO_SNDBUF/SO_RCVBUF), without this sometimes its not possible to reach the best throughput with the default buffer sizes (I don't know why, found this out by experimenting). But this is not a parameter that decides between async/blocking. Its rather the protocol. When do you send and when do you receive data? If you can exactly determine when to receive and when to send then use blocking sockets because its easier (the typical protocol for blocking sockets is request - response, one of the peers is active and the other just replies). If you might want to send at arbitrary points and you might have to receive incoming packets at random points then you need to write an async solution that also handles when the bandwith isn't good enough to send data with the desired rate.

EDIT: Transferring data from one thread to another one: this stuff is called blocking queue. It is the most universal and often the most efficient way to send data/events from one thread to another. The worker thread has a blocking queue and it always gets the first item from it, if the queue is empty then the worker thread blocks on the get() method of the queue until another thread puts an item to it. After getting the item from the queue the worker thread processes that item (sends data on the socket) and then it goes back to the beginning of the loop and gets another item from the loop for processing. The other thread or threads can put data to the other side of the queue. The put method doesn't block. You should just store pointers in this queue, pointers to data objects to minimize the synchronization overhead (by avoiding data copying) in the blocking queue. Unfortunately I have no favorite threading tutorial but I'm currently working on a codeproject article about threading for dummies. Smile | :) That will describe threading step by step and will teach using blocking queues too.

EDIT: Since in your case the data flow goes to a single direction you should use a blocking socket. You should also handle somehow when your bandwidth goes too low, in that case you should avoid the blocking queue from growing ultra large causing an out of memory error. You should somehow suspend the generation of messages in that case because sooner or later you will not have space to store the newly generated messages.

EDIT: A very simple example to using a multi-producer single consumer blocking message queue (with only a single producer thread - the main thread):
C++
#include <windows.h>
#include <stdio.h>
#include <assert.h>
#include <queue>

template <typename T>
class BlockingQueue
{
public:
	BlockingQueue()
	{
		InitializeCriticalSection(&m_Lock);
		m_hBecameNonEmptyEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
	}

	~BlockingQueue()
	{
		assert(m_Queue.empty());
		DeleteCriticalSection(&m_Lock);
		CloseHandle(m_hBecameNonEmptyEvent);
	}

	// Works well with many producer threads.
	void Put(const T& item)
	{
		EnterCriticalSection(&m_Lock);
		if (m_Queue.empty())
			SetEvent(m_hBecameNonEmptyEvent);
		m_Queue.push(item);
		LeaveCriticalSection(&m_Lock);
	}

	// You should call this with only one consumer thread.
	void Get(T& item)
	{
		EnterCriticalSection(&m_Lock);
		if (m_Queue.empty())
		{
			ResetEvent(m_hBecameNonEmptyEvent);
			LeaveCriticalSection(&m_Lock);
			WaitForSingleObject(m_hBecameNonEmptyEvent, INFINITE);
			EnterCriticalSection(&m_Lock);
		}

		item = m_Queue.front();
		m_Queue.pop();
		LeaveCriticalSection(&m_Lock);
	}

private:
	BlockingQueue(const BlockingQueue&);
	BlockingQueue& operator=(const BlockingQueue&);

private:
	CRITICAL_SECTION m_Lock;
	HANDLE m_hBecameNonEmptyEvent;
	std::queue<T> m_Queue;
};

BlockingQueue<int> q;

DWORD __stdcall ThreadProc(void* param)
{
	printf("thread started\n");
	for (;;)
	{
		int i;
		q.Get(i);
		printf("processing %d\n", i);
		if (i == 0)
			break;
	}
	printf("thread is out of the loop, returning from threadproc...\n");
	return 0;
}


int main(int argc, char* argv[])
{
	printf("Creating thread...\n");
	DWORD thread_id;
	HANDLE hThread = ::CreateThread(NULL, 0, ThreadProc, NULL, 0, &thread_id);
	if (!hThread)
	{
		fprintf(stderr, "CreateThread error!\n");
		return 1;
	}

	for (;;)
	{
		char buf[0x100];
		if (!gets(buf))
			break;
		int i;
		if (1 == sscanf(buf, "%d", &i))
		{
			q.Put(i);
			if (i == 0)
				break;
		}
		else
		{
			printf("Not an integer: %s\n", buf);
		}
	}

	printf("Waiting for the thread to finish...\n");
	WaitForSingleObject(hThread, INFINITE);
	printf("Wait finished.\n");
	CloseHandle(hThread);
	return 0;
}

You should store pointers in the blocking queue and you can use for example the NULL pointer to ask the other thread to exit like I used the zero value.

modified 27-Aug-12 16:11pm.

GeneralRe: from sync TCP/IP to async Pin
jschell27-Aug-12 8:24
jschell27-Aug-12 8:24 
QuestionAccessing extra keys on a PS2 mouse without driver Pin
David Gvozdenovic26-Aug-12 4:57
David Gvozdenovic26-Aug-12 4:57 
AnswerRe: Accessing extra keys on a PS2 mouse without driver Pin
pasztorpisti26-Aug-12 5:41
pasztorpisti26-Aug-12 5:41 
QuestionCreateProcess and Detect when tthis process Ends.. Pin
Drakesal26-Aug-12 1:25
Drakesal26-Aug-12 1:25 
AnswerRe: CreateProcess and Detect when tthis process Ends.. Pin
pasztorpisti26-Aug-12 3:18
pasztorpisti26-Aug-12 3:18 
AnswerRe: CreateProcess and Detect when tthis process Ends.. Pin
Stephen Hewitt26-Aug-12 5:01
Stephen Hewitt26-Aug-12 5:01 
GeneralRe: CreateProcess and Detect when tthis process Ends.. Pin
Drakesal26-Aug-12 21:07
Drakesal26-Aug-12 21:07 
QuestionConvet Perl regular expression to equivalent ECMAScript regular expression Pin
Falconapollo25-Aug-12 18:49
Falconapollo25-Aug-12 18:49 
Questionexception caused by reference to certain Windows functions Pin
ed welch24-Aug-12 6:12
ed welch24-Aug-12 6:12 
AnswerRe: exception caused by reference to certain Windows functions Pin
pasztorpisti24-Aug-12 9:47
pasztorpisti24-Aug-12 9:47 
AnswerRe: exception caused by reference to certain Windows functions Pin
Stephen Hewitt24-Aug-12 21:15
Stephen Hewitt24-Aug-12 21:15 
GeneralRe: exception caused by reference to certain Windows functions Pin
ed welch25-Aug-12 2:11
ed welch25-Aug-12 2:11 
GeneralRe: exception caused by reference to certain Windows functions Pin
Richard MacCutchan25-Aug-12 4:21
mveRichard MacCutchan25-Aug-12 4:21 
GeneralRe: exception caused by reference to certain Windows functions Pin
ed welch25-Aug-12 5:33
ed welch25-Aug-12 5:33 
GeneralRe: exception caused by reference to certain Windows functions Pin
Stephen Hewitt25-Aug-12 23:39
Stephen Hewitt25-Aug-12 23:39 
AnswerRe: exception caused by reference to certain Windows functions Pin
jschell25-Aug-12 7:37
jschell25-Aug-12 7:37 
GeneralRe: exception caused by reference to certain Windows functions Pin
ed welch25-Aug-12 9:19
ed welch25-Aug-12 9:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.