What is wrong with my program

I am trying to run two threads simultaneously wherein one thread copies data from a file into an array and the other thread simply prints out a statement. I am using semaphores so each thread gets chance to do its job. But all my program is doing is executing the thread using the "Consumption" function. Seems like the thread using the "Summation" function is completely neglected. Why is this happening?

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <windows.h>
#include <stdio.h> 
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;

class Semaphore {
private:
	int value;
public:
	Semaphore(int v) 
	{
	 value = v; 
	}
       void up() 
	   { 
			value++;
		}
       void down() 
	   { 
			value--; 
		}
};

Semaphore mutex(1);
Semaphore full(0);
Semaphore empty(19);
DWORD Sum;
int ARRAY[20];

DWORD WINAPI Summation(LPVOID Param)
{
	int count = 0;
	int bound, random_number;
	unsigned seed = time(0);
	//Seed The Random Number Generator
	srand(seed);
	ifstream inputFile;
	inputFile.open("Input.txt");
	while(!inputFile.eof())
	{
		empty.down();
	    mutex.down();
		cout << "Specify An Upper Bound For The Number Of Bytes To Be Copied" << endl;
		cin >> bound;
		random_number = 1 + rand() % bound;
		cout << "N = " << random_number << endl;
		int count = 0;
		//Reading data from the file
		while (count < random_number && inputFile >> ARRAY[count]){
			count++;
		}
		mutex.up();
		full.up();
	}//endwhile
	DWORD Upper = *(DWORD*) Param;
	for (DWORD i = 0; i <= Upper; i++)
		Sum += i;
	return 0;
}

DWORD WINAPI Consumption(LPVOID Param)
{
	while(1)
	{
		full.down();
		mutex.down();
		cout << "This Is Consumption: " << endl;
		mutex.up();
		empty.up();
	}//endwhile
	return 0;
}

int main()
{
	DWORD ThreadId;
	DWORD ThreadId2;
	HANDLE ThreadHandle;
	HANDLE ConsumerHandle;
	int Param = 5;
	int Param2 = 6;

	//Create The Threads
	ThreadHandle = CreateThread(NULL, 0, Summation, &Param, 0, &ThreadId);
	ConsumerHandle = CreateThread(NULL, 0, Consumption, &Param2, 0, &ThreadId2);
   
	if(ThreadHandle != NULL){
		WaitForSingleObject(ThreadHandle,INFINITE);

		CloseHandle(ThreadHandle);
	}
	
	return 0;
}
Last edited on
Does you code run OK with a trivial Summation function?

I'm not sure that your home made semaphore actually ever actually locks. I would probably modify your code to use a WIN32 CRITICAL_SECTION.

But if you do want to code your own semaphor, you could have a go with Dekker's or Peterson's algorithms.
http://en.wikipedia.org/wiki/Dekker%27s_algorithm
http://en.wikipedia.org/wiki/Peterson%27s_algorithm

Andy

P.S. Just seen your other post: if you need to use a semaphor, use CreateSemaphore(), ... rather than InitializeCriticalSection(), ...
Last edited on
Just spotted the cin in your Summation function (line 46).

So it's going to be waiting for input while the other thread carries on!

But it should still print out line 45. That way I would, at least, know it is not treating Summation as invisible
It does (print out line 45) for me. But it wings past!

I checked by setting a breakpoint on the line.
Hmm. So that means, I should probably have the main ask the user for input, and then have the input value passed as argument to "Summation". Also do you think the semaphores look shady? As you pointed out before, the critical section of each process might not actually be getting a lock with my implementation of semaphores.
I don't thing your semaphor class works. I recommend you replace it with the WIN32 semaphore.

And yes, I think asking the user in main for the max copy size and passing it to the thread makes sense.
Topic archived. No new replies allowed.