コード例 #1
0
        public ThreadComputationQueue(uint concurrentThreads)
        {
            ConcurrentThreads = concurrentThreads;
            Agent             = Agent <AgentMsg, Unit> .Start((uint)0,
                                                              (inbox, threads) =>
            {
                var msg = threads < ConcurrentThreads
                        ? inbox.Receive().RunSynchronously()
                        : inbox.Scan(m => m is CompletedComputation).RunSynchronously();

                return(Pattern <uint>
                       .Match(msg)
                       .Case <EnqueueComputation>(c =>
                {
                    new Thread(() =>
                    {
                        try
                        {
                            c.Computation.RunSynchronously();
                            Agent.Post(new CompletedComputation());
                        }
                        catch (Exception exception)
                        {
                            Agent.Post(new CompletedComputation());
                            Async.DispatchException(exception);
                        }
                    }).Start();

                    return threads + 1;
                })
                       .Case <CompletedComputation>(_ => threads - 1)
                       .Default(threads)
                       .Run());
            });
        }
コード例 #2
0
 public Unit Enqueue(Async <Unit> a)
 {
     ThreadPool.QueueUserWorkItem(_ => {
         try
         {
             a.RunSynchronously();
         }catch (Exception e)
         {
             Async.DispatchException(e);
         }
     });
     return(Unit.Default);
 }
コード例 #3
0
        /// <summary>
        ///     Creates and starts an agent.
        /// </summary>
        /// <typeparam name="TState">The type of the state.</typeparam>
        /// <param name="state">The initial state.</param>
        /// <param name="body">The body to be executed.</param>
        /// <returns>The agent.</returns>
        public static Agent <TMessage, TReply> Start <TState>(TState state, Func <Agent <TMessage, TReply>, TState, TState> body)
        {
            var agent = new Agent <TMessage, TReply>();

            new Thread(_ =>
            {
                try
                {
                    Watchdog(agent, body, state).RunSynchronously();
                }
                catch (Exception e)
                {
                    Async.DispatchException(e);
                }
            }).Start();
            return(agent);
        }
コード例 #4
0
 /// <summary>
 ///     Posts a message to the message queue of the Agent, asynchronously.
 /// </summary>
 /// <param name="msg">The message to post.</param>
 public Unit Post(TMessage msg)
 {
     new Thread(() =>
     {
         lock (_inbox)
         {
             try
             {
                 _inbox.Enqueue(msg);
                 Monitor.Pulse(_inbox);
             }
             catch (Exception e)
             {
                 Async.DispatchException(e);
             }
         }
     })
     .Start();
     return(Unit.Default);
 }