C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-13-2007, 01:05 PM   #1
Registered User
 
Join Date: Dec 2006
Posts: 26
Question about Multiple threads and ring buffer.

Dear all,

My program scenario is as follows.

Three threads (thread 1-3) will fetch values from the ring buffer. E.g. after one thread gets one value from ring buffer of which length is 100, the outputIndex of ring buffer will be decreased by one, per time.

Meanwhile, there is anther thread (thread 4), which has the functionality of re-filling the ringbuffer. E.g. when OutputIndex of ringbuffer equals to 0, the thread will start to re-fill the ringbuffer. Otherwise, it will be blocked/suspended.

My question is that how shall I play this scenario. I am newbie in thread, and there are not so many discriptive and simple thread programs. I wrote a snippet of code as follows. For my code, Although ringbuffer.outputIndex equals to 0, it is never initialized by thread 4. Is there anyone can help me a little?

Code:
DWORD WINAPI init_ringbuffer() 
{
 // ring buffer initialization.
 ...
}

void createThread_4(void)
{
	HANDLE Handle_Of_Thread_4;
	
        Handle_Of_Thread_4 = CreateThread( NULL, 
					   0, 
					   init_ringbuffer, 
					   NULL, 
					   CREATE_SUSPENDED, 
					   NULL);  
    	
	if (ringbuffer.outputIndex == 0)
	{
		ResumeThread( Handle_Of_Thread_4 );
	}
	else
	{
		//Keep thread blocked.
	}
	
}

int main() 
{
    // Declaration of ring buffer.

    createThread123(); 
    createThread_4();

    // Wait for all the threads terminate, and close them.
    ...


}
qingxing2005 is offline   Reply With Quote
Old 01-13-2007, 02:21 PM   #2
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
1. Write the cyclic buffer manager. It should contain read function, write function maybe some others.
2. Test it in the single thread environment (check that there is no memory overruns, missing data etc)
3. Add the memory locking mechanis (critical section I suppose) that will wrap any operation involving buffer state change
4. Test it in 2 thread environment: 1 filling buffer, one reading from the buffer (check that no locks occur during nights run for example)
5. Implement your real scenario with 4 threads
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 01-15-2007, 12:30 AM   #3
Registered User
 
Join Date: Dec 2006
Posts: 26
Re: Resuming thread issue.

Dear vart,

Thansk for your reply and suggestions. They are really helpful.

However, I still met one further problem about resuming the thread. The thread function init() will be executed when the thread test_thread() is resumed. However, it is never done by my following snippet of code as follows. I don't know where the problem is. Can you help me a little?

Thanks in advance,

Bests,

L.M

Code:
// Include relevant libs.
#include "windows.h"
...

//global vars.
HANDLE Handle_1 = 0;

DWORD WINAPI init()
{
	...
}

void test_thread(void)
{
	DWORD dwThreadId;
	
	Handle_1 = CreateThread( NULL, 0, init, NULL, CREATE_SUSPENDED, &dwThreadId );
												
}

int main() 
{
     ...
     test_thread();
     ResumeThread( Handle_1 );
     ...
}
qingxing2005 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multithreading (flag stopping a thread, ring buffer) volatile ShwangShwing C Programming 3 05-19-2009 07:27 AM


All times are GMT -6. The time now is 05:52 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22