'Where could be any problems in the program and how i solve them with using lock() and unlock()?
The following piece of pseudo-code shows a typical reader / writer scenario:
string document;
string::size_type length = 0;
write()
{
while (true) {
string text = readFromKeyboard();
document.append(text);
length = length + text.length();
}
}
read()
{
static string::size_type pos = 0;
while (true) {
if (pos < length) {
process(document.substr(pos, length - pos));
pos = length - 1;
}
}
}
main()
{
unsigned int k = 20;
while (k--)
Thread consumer(read).start;
Thread producer(write).start;
wait();
}
My question is:
Where can concurrent execution problems occur in this program?
And how they should be protected only using the pseudo-code functions lock ()and unlock ()?
Solution 1:[1]
There is not much known about your code but I'm assuming, that neither document nor length are atomic. What you would need here is a distinction between write access and read access (assuming that read access is const). Writing will change document and length and must be protected from other accesses. Reading must be protected from changes by the write call, but since reading does not alter neither document nor length it is allowed to be done in multiple threads at once.
I'm taking the liberty to use lock_write() and lock_read(). Doing this with full lock() calls, would make most read-threads useless. Also, I'm taking the liberty to fix this pos = length - 1-thing you have in your read() function.
write() will become:
write()
{
while (true) {
string text = readFromKeyboard();
lock_write();
document.append(text);
length = length + text.length();
unlock();
}
}
And read() will become:
read()
{
static string::size_type pos = 0;
while (true) {
lock_read();
if (pos < length) {
process(document.substr(pos, length - pos));
pos = length;
}
unlock();
}
}
Also, read() will go into a busy wait, which is not nice. This could be fixed, using a condition variable.
Solution 2:[2]
One method I thought of:
If you go around the HSV color wheel at equal intervals then you get distinct colors. HSV are in the range of [0,1]. Set Saturation and Value to 1 unless you need more than a few dozen colors.
Method to create an array of the amount of colors you would like:
const colorCount = 24;
const dh = 1 / colorCount;
let colors = [];
for(let i=0;i<colorCount;i++) {
let rgb = HSVtoRGB(dh*i,1,1);
colors.push(rgb);
}
// And our helper function:
/* accepts parameters
* h Object = {h:x, s:y, v:z}
* OR
* h, s, v
*/
function HSVtoRGB(h, s, v) {
var r, g, b, i, f, p, q, t;
if (arguments.length === 1) {
s = h.s, v = h.v, h = h.h;
}
i = Math.floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
return {
r: Math.round(r * 255),
g: Math.round(g * 255),
b: Math.round(b * 255)
};
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Xoozee |
| Solution 2 | Euthyphro |

