Hi all, hope everyone is well ;)
I am "experimenting" (;)) with some "thread" things, and, I have some code:
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
|
int _tmain(int argc, _TCHAR* argv[])
{
_TCHAR* tcthrea = argv[1];
int its = atoi((char*)tcthrea);
bool bgo = true;
bool *pbgo = &bgo;
for(int i = 0; i < 100; /*i++*/)
{
std::vector<std::thread*> ts;
int j = 0;
for (j = 0; j <its && i+j < 100; j++)
{
//std::wcout << "starting thread " << i+j << " file: " << i+j << std::endl;
std::thread *thread = new std::thread(filetodb, pbgo,j);
ts.push_back(thread);
}
for(j = 0; j < its && i+j < 100; j++)
{
ts.at(j)->join();
delete ts.at(j);
}
i = i+j;
}
return 0;
}
|
int its
(the argument, passed, when starting the executable, is the number of threads, I want, at one time.
each thread calls/is a call, to
int filetodb(bool *pbgo, int its);
, which, is
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
|
int filetodb(bool *pbgo, int its)
{
if (*pbgo == false)
{
std::wcout << "thread: " << its << " waiting " << std::endl;
}
while (*pbgo == false)
{
continue;
}
*pbgo = false;
for (int j = 0; j < 10000; j++)
{
//*************
//do some "critical" stuff here
//*************
if (j%100 == 0)
{
std::wcout << j << std::endl;
}
continue;
}
*pbgo = true;
return 0;
}
|
this, in the loop, has a section "critical", i.e, a section, where calls are made, that can NOT be made "synchronously" (i.e., at the "same" time), as the same calls, in other threads that call filetodb.
So, I started using a "semaphore", the
bool *pbgo
.
At the beginning, pbgo (i.e., pointer bool for go) is initialized to "true" (see tmain). In each thread, if pbgo is false, it prints "thread x waiting", where x, is the thread number, and, then, loops, in the while statement, until pbgo is no longer false. This happens, when the "critical" code is done (at, the end, of the loop, in filetodb).
Not sure, whether this is the "best" way of dealing with concurrency, but, I wanted to give it a shot.
So, this works, using 1, 2, or x threads, using "debug" version (visual studio).
As soon as this is in release, thois starts "not working", correctly.
the exe is called "application.exe".
if I do, in cmd:
application.exe 1
it works.
If I do, in cmd, however:
application.exe 2
it outputs:
0
100
200
300
400
500
600
700
800
900
1000
1100
1200
1300
1400
1500
1600
1700
1800
1900
2000
2100
2200
2300
2400
2500
2600
2700
2800
2900
3000
3100
3200
3300
3400
3500
3600
3700
3800
3900
4000
4100
4200
4300
4400
4500
4600
4700
4800
4900
5000
5100
5200
5300
5400
5500
5600
5700
5800
5900
6000
6100
6200
6300
6400
6500
6600
6700
6800thread: 1 waiting
6900
7000
7100
7200
7300
7400
7500
7600
7700
7800
7900
8000
8100
8200
8300
8400
8500
8600
8700
8800
8900
9000
9100
9200
9300
9400
9500
9600
9700
9800
9900
^C
|
I.e., it starts thread 0, counts, and, somewhere, at the middle, it reaches, in thread 1, the same, "critical" section, and, prints "thread 1 waiting".
As expected.
meanwhile, thread0, keeps on counting, but, when it has reached the end, even though pgbo is being set back to true, it hangs, as you can see, at the end, of output, where one needs to, then, cancel the application, using ctrl+c.
So, this is, a rather "longer" (;)) story, and, again, there are probably different ways of dealing with this (threads, concurrency, etc), but, I am wondering: why does this seem to work in debug, for any input # of threads, but, in release, only for the "1-thread-case" (i.e., basically, as if these calls were just made, in an iterative loop, i.e., no "real" "threading")!
Thanks!
Regards!
C