by Anonymous » 22 Aug 2025, 06:50
Code: Select all
class Sample
{
int a = 0;
public void Run()
{
// main thread.Assuming this is chromium task runner.
auto currentRunner = GetCurrentDefault();
somePooledRunner->PostTask(
[currentRunner]()
{
a = TimeConsumingOperation();
// post back to main thread.
currentRunner->PostTask([]()
{
OnComplete1(a);
OnComplete2();
});
});
}
void OnComplete1(int a)
{
Process(a);
}
void OnComplete2()
{
Process(this.a); // Is there a data visibility issue here that I'm overlooking but is being handled correctly by the library I'm using?
}
}
Dies ähnelt der SynchronisationContext.post für Async/Awit. Die lockfree- oder sperrenbasierte Implementierung von Task-Warteschlangen hilft mir, diese Probleme korrekt zu behandeln. Aber wenn ich diese beiseite legte und dieses
Problem vorliegt, frage ich mich, ob es hier wirklich ein
Problem gibt. < /P>
Code: Select all
class SimpleSample
{
int a = 0;
void Run()
{
auto t = std::thread([this]()
{
a = TimeConsumingOperation();
});
t.detach();
// To simplify the problem, no synchronization mechanism is introduced, and only sleep is performed.
Sleep(enoughTime);
// Assume that the sleep time is sufficient for TimeConsumingOperation to complete and write a in worker thread. Is there a possibility that I cannot obtain the calculation result of a due to data visibility?
printf(a);
}
}
Gibt es eine Möglichkeit, dass ich das Berechnungsergebnis eines aufgrund der Datensäußerung in
Simpleable ?
[code]class Sample
{
int a = 0;
public void Run()
{
// main thread.Assuming this is chromium task runner.
auto currentRunner = GetCurrentDefault();
somePooledRunner->PostTask(
[currentRunner]()
{
a = TimeConsumingOperation();
// post back to main thread.
currentRunner->PostTask([]()
{
OnComplete1(a);
OnComplete2();
});
});
}
void OnComplete1(int a)
{
Process(a);
}
void OnComplete2()
{
Process(this.a); // Is there a data visibility issue here that I'm overlooking but is being handled correctly by the library I'm using?
}
}
[/code]
Dies ähnelt der SynchronisationContext.post für Async/Awit. Die lockfree- oder sperrenbasierte Implementierung von Task-Warteschlangen hilft mir, diese Probleme korrekt zu behandeln. Aber wenn ich diese beiseite legte und dieses [url=viewtopic.php?t=26065]Problem[/url] vorliegt, frage ich mich, ob es hier wirklich ein [url=viewtopic.php?t=26065]Problem[/url] gibt. < /P>
[code]class SimpleSample
{
int a = 0;
void Run()
{
auto t = std::thread([this]()
{
a = TimeConsumingOperation();
});
t.detach();
// To simplify the problem, no synchronization mechanism is introduced, and only sleep is performed.
Sleep(enoughTime);
// Assume that the sleep time is sufficient for TimeConsumingOperation to complete and write a in worker thread. Is there a possibility that I cannot obtain the calculation result of a due to data visibility?
printf(a);
}
}
[/code]
Gibt es eine Möglichkeit, dass ich das Berechnungsergebnis eines aufgrund der Datensäußerung in [b] Simpleable [/b] ?