Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Coffee corner » Quiz #4
Quiz #4 [message #57802] Sat, 18 December 2021 09:21 Go to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Now for something much more fun:

CONSOLE_APP_MAIN
{
	int x = 0;
	CoDo([&] {
		CoWork::FinLock();
		x++;
	});
	DDUMP(x);
}


What can you say about the number printed to the log?
Re: Quiz #4 [message #57803 is a reply to message #57802] Sat, 18 December 2021 10:24 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1091
Registered: August 2007
Senior Contributor
Quote:
Now for something much more fun:

CONSOLE_APP_MAIN
{
int x = 0;
CoDo([&] {
CoWork::FinLock();
x++;
});
DDUMP(x);
}



What can you say about the number printed to the log?


Ideally it should be 1.

But in this case the result should (or expected to) be "0 < x <= INT_MAX", depending on the underlying hardware/OS task/process scheduling?

Best regards,
Oblivion



Re: Quiz #4 [message #57804 is a reply to message #57803] Sat, 18 December 2021 14:08 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Oblivion wrote on Sat, 18 December 2021 10:24
Quote:
Now for something much more fun:

CONSOLE_APP_MAIN
{
int x = 0;
CoDo([&] {
CoWork::FinLock();
x++;
});
DDUMP(x);
}



What can you say about the number printed to the log?


Ideally it should be 1.

But in this case the result should (or expected to) be "0 < x <= INT_MAX", depending on the underlying hardware/OS task/process scheduling?

Best regards,
Oblivion



Incorrect.
Re: Quiz #4 [message #57810 is a reply to message #57804] Sat, 18 December 2021 21:21 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1091
Registered: August 2007
Senior Contributor
OK. What am I missing here?

AFAIK,

- CoDo() function is blocking, i.e. it will wait for the threads to join, before it exits.
- CoDo() calls CoWork::Loop(). The API document states that:

Schedules fn to be run on all worker threads and on calling thread. After the first thread returns from fn, all other scheduled fn jobs that has not started yet are unscheduled. Waits for all started jobs to finish.


Best regards,
Oblivion


Re: Quiz #4 [message #57811 is a reply to message #57810] Sun, 19 December 2021 09:28 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
You are not completely wrong.

1 really is the most probable result with current implementation. This is basically pure race condition - how many worker threads will CoDo unblock until it registers that first of them finished.

Anyway, INT_MAX is definitely a little bit high - the teoretical maximum is the number of worker threads.

Mirek

[Updated on: Sun, 19 December 2021 09:29]

Report message to a moderator

Previous Topic: Little Quiz
Next Topic: Quiz #3
Goto Forum:
  


Current Time: Thu Mar 28 16:21:59 CET 2024

Total time taken to generate the page: 0.01582 seconds