Goal: Spin up 20 threads that will all hit the SessionFactory.GetSessionFactory(key)
method at the same time for testing purposes. (I'm trying to simulate a multi-threaded environment such as ASP.NET)
Question: By using the EndInvoke()
method am I essentially calling the GetSessionFactory(key)
method synchronously or is my code correct in simulating multiple threads all hitting GetSessionFactory(key)
at the same time?
Thanks,
Kyle
public void StressGetSessionFactory()
{
for (int i = 0; i < 20; i++)
{
Func<string, ISessionFactory> method = GetSessionFactory;
IAsyncResult asyncResult = method.BeginInvoke("RBDB", null, null);
ISessionFactory sessionFactory = method.EndInvoke(asyncResult); //My concern is with this call
Debug.WriteLine("RBDB ISessionFactory ID: " + sessionFactory.GetHashCode());
}
}
static ISessionFactory GetSessionFactory(string key)
{
return SessionFactory.GetSessionFactory(key);
}
In taskmanager, right-click the game process and set the affinity to one core. Play a little ingame and check your fps. Then change affinity to two cores, if your fps increases then the game is (properly) multithreaded.
C/C++ Languages Now Include Multithreading Libraries Moving from single-threaded programs to multithreaded increases complexity. Programming languages, such as C and C++, have evolved to make it easier to use multiple threads and handle this complexity. Both C and C++ now include threading libraries.
Python doesn't support multi-threading because Python on the Cpython interpreter does not support true multi-core execution via multithreading. However, Python does have a threading library.
Multi-threaded programming is probably the most difficult solution to concurrency. It basically is quite a low level abstraction of what the machine actually does. There's a number of approaches, such as the actor model or (software) transactional memory, that are much easier.
IAsyncResult asyncResult = method.BeginInvoke("RBDB", null, null);
ISessionFactory sessionFactory = method.EndInvoke(asyncResult);
It's not parallel, from MSDN:
The EndInvoke method is used to retrieve the results of the asynchronous call. It can be called any time after BeginInvoke; if the asynchronous call has not completed, EndInvoke will block until it completes.
To make parallel calls you could use a higher level abstraction (like the TPL i.e. would give you all of this almost for free) or slightly refactor your code - make the asynchronous calls first, then collect the results afterwards (untested):
IAsyncResult[] asyncResult = new IAsyncResult[20];
ISessionFactory[] sessionFactories = new ISessionFactory[20];
Func<string, ISessionFactory> method = GetSessionFactory;
for (int i = 0; i < 20; i++)
{
asyncResult[i] = method.BeginInvoke("RBDB", null, null);
}
for(int i = 0; i < 20; i++)
{
sessionFactories[i] = method.EndInvoke(asyncResult[i]);
}
Note that with BeginInvoke()
you do not have a guarantee that you are in fact making 20 calls in parallel, since it uses the thread pool it will parallelize as it sees fit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With