Contention Issue without Threads

I have written the following piece of code to simulate an ad-hoc network and I have run into a problem that suggests that I am having a contention issue

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <thread>
#include <cstdlib>
#include <memory>
#include <fstream>

#include "Functions_v4.h"

const int NUMNOD = 200; 	// number of mobile nodes 
const int MAXBAT = 100;		// battery charge 
const int DIM_X = 30;           // number of columns in grid (plus two extra columns for fixed nodes)
const int DIM_Y = 30;           // number of rows in grid
const int NUM_SLAVES = 2;       // number of slaves that a master must have before accepting a bridge

const float SD = 0.001;		// standard deviation in node walk

int main(int argc, char* argv[])
{
        // output file
        std::ofstream file1;
	file1.open("ScatternetStats.dat");

	// starts pgplot
	cpgopen("/GW");
	//cpgopen("fig4.ps/PS");

	/* cpgenv adjusts scales from 0 to XMAX and from 0 to YMAX. The 5th value
	* (1) indicates that thre is no relation between the 'x' and 'y' axis scales,
	* the 6th value indicates that only a box is drawn */
	cpgenv(0.0, DIM_X + 1, 0.0, DIM_Y + 1, 1, -1);

	// grid in which the nodes will move
	matrix<CNode*> Grid(DIM_X, DIM_Y, 0);

	// list of nodes
	std::vector<CNode*> NodeList;

	// output file header
	file1 << "numNodes" << '\t' << "density" << '\n';

	// initial number of nodes
	int n = 10;

	// increment in number of nodes
	int deltaNUM = 5;

	// number of nodes is progressively increased
	while(n <= NUMNOD)
	{
		// accumulated average # of slaves per master
		float acc = 0;

		// draws grid and presents initial distribution of nodes
		for(int j = 1; j < MAXBAT; j++)
		{
			rand_init();

			////////////////////////////////////////////////
			// nodes are generated and placed on the grid //
			////////////////////////////////////////////////
			GenerateNodes(Grid, NodeList, n);

			///////////////////////
			// builds scatternet //
			///////////////////////
			BuildScatternet(Grid, NodeList, NUM_SLAVES);
		
			////////////////////////////////
			// clears screen & draws grid //
			////////////////////////////////
			DrawGrid(DIM_X, DIM_Y);
				
			////////////////////////////
			// presents nodes on grid //
			////////////////////////////
			ShowNodes(NodeList);

			///////////////////////////////////
			// Computes stats for scatternet //
			///////////////////////////////////
			//std::this_thread::sleep_for(std::chrono::milliseconds(1000));

			float dens = computeDensity(NodeList);
			std::cout << dens << std::endl;
			acc += dens;
		
			///////////////////////////////
			// clears node list and grid //
			///////////////////////////////
			NodeList.clear();
			Grid.clear();
		}

		file1 << n << '\t' << acc/static_cast<float>(MAXBAT) << '\n';

		std::cout << "avg for " << n << " nodes = " << acc/static_cast<float>(MAXBAT) << std::endl;

		// increases number of nodes
		n += deltaNUM;

	}

	

	// frees memory
	for(int n = 0; n < DIM_X; n++)
	{
		for(int m = 0; m < DIM_Y; m++)
		{
			delete Grid(m,n);
		}
	}
		
	// closes pgplot
	cpgend();

	//file1.flush();
	file1.close();

	return 0;

}		


The problem I am experiencing occurs in lines 80-83: if I comment line 80 (the one introducing a 1 second delay) the result from line 82 is not updated for every iteration of the for loop: I get the same value calculated in the first iteration repeated over and over. However, when using the debugger and introducing a breakpoint at line 83 the value of dens is correctly updated for each iteration. How can this be? To the best of my knowledge I am not declaring any threads here (and I know very little about them) so I shouldn't have any contention issues: it seems as if the execution of ShowNodes in line 75 is still in process when line 82 is reached. Based on this hunch I introduced the 1 second delay in line 80: with this modification dens is correctly updated in every iteration but of course the code is now annoyingly slow.

Could someone please tell me what is going on here and how can I fix it without the sleep instruction in line 80? I am using MSVS 2012 Express for Desktop, version 11.0.61030.00, Update 4.

Cheers,
Little
Last edited on
This usually hints at a [silent] crash before that line
Last edited on
Topic archived. No new replies allowed.