Running functions in the background

Greetings!
I am not sure if this should be here or at the Beginners' forum but, it's a bit complicated in my opinion (at least for me), and since my English is not perfect, It's easier for me to demonstrate, so here goes:

My code so far:

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

class Pool
{
public:
	int fish;
	Pool() {};
	Pool(int);
	~Pool() {};
	void getFish(int);
};

Pool::Pool(int numFish)
{
	this->fish = numFish;
}

void Pool::getFish(int caughtFish)
{
	this->fish = this->fish - caughtFish;
}


class Fisher
{
        Pool *selectedPool;
public:
	int fish;
	Fisher() {};
	Fisher(Pool*);
	~Fisher() {};
	void cast(int);
};

Fisher::Fisher(Pool *fishingPool)
{
    fish = 0;
    selectedPool = fishingPool;
}

void Fisher::cast(int luck)
{
    selectedPool->getFish(luck);
    this->fish = this->fish + luck;
}

int main(int argc, char **argv)
{
    
    srand((unsigned)time(0));
    Fisher *fisher1, *fisher2;
    Pool *lake1 = new Pool(100);
    fisher1 = new Fisher(lake1);
    fisher2 = new Fisher(lake1);
    cout << "Fishing contest starts..." << endl;
    while (lake1->fish > 0)
    {
        fisher1->cast(rand()%10);
        fisher2->cast(rand()%10);
    }
    cout << "Contest ended!" << endl
            << "Fisherman 1 got " << fisher1->fish << " fish." << endl
            << "Fisherman 2 got " << fisher2->fish << " fish." << endl;
    
    if (fisher1->fish > fisher2->fish) { cout << "Fisherman 1 wins!" << endl; }
    else { cout << "Fisherman 2 wins" << endl; }
    
    return 0;
}


I want to add time to the cast function, a random value between 2 and 10 seconds, and make the fishermen cast together at the same time.
I've searched trough Google for awhile and the conclusion I've came up with is that I need to use threads. Is this true, or there's another way to accomplish this, as I want the program to be cross-platform and to stay away from platform-specific stuff ? Thanks for your replies!

P.S. I know that right now the total caught fish can exceed the initial fish in the pool, but that's not the problem :)
closed account (S6k9GNh0)
There are *tons* of platform-agnostic threading libraries. They're literally over-implemented. C++11 has std::thread, Boost gives boost::thread, and many more that I don't care to list. Hell, even pthreads has an implementation on Wind0w$ and MacOS (although I've heard they're terribly buggy). Threads are a more convenient solution to more things than you think.

I'll give some rather good advice though. Threads are not meant to be spawned at every chance you get. There real advantage is actually taking advantage of multi-core processors. They tend to be heavy to spawn. You generally spawn a thread pool of said amount of CPU cores, then queue actions for that thread pool to do, instead of consistently recreating threads. Because your example is so small, it probably doesn't matter.

Last edited on
Thanks for the reply, does this means that the only way to do it is threading? Someone mentioned a task-based implementation, which is preferred over a thread-based one ?
Could you give me some light on this? Thanks again!
closed account (S6k9GNh0)
Herm, I looked up online the common definition of "task" and it's rather abstract in the context you use it.

Some refer to a "task" as a process such as the approach used in the overly retarded fork() approach. Some refer to a "task" as what you throw onto a thread pool to execute it asyncronously. I believed that the former was the only definition although it seems I'm wrong..

Creating your own process has very few benefits. It's incredibly heavy and complicated for the trivial tasks. Hell, I'm not sure I can ever recommend it since I don't think it's ever ideal.

A thread on the other hand is more lite and less complicated for trivial tasks and gets *very* complicated in larger applications. You must deal with race conditions and you have to be very careful about it. Tracking problems within a multi-threaded application is extremely tricky, even with complicated use of a debugger. The best way to go about it is to have very strict conditions in which a thread function be performed.

There's also something called the "fiber" which takes even less resources during startup but is generally slow. This is why the "thread pool" approach tends to be more virtuous. A fiber tends to be easier to work with though. I'm not aware of any cross-platform fiber library as they don't tend to be useful. http://msdn.microsoft.com/en-us/library/windows/desktop/ms682661%28v=vs.85%29.aspx
Last edited on
Thank you!
Topic archived. No new replies allowed.