Pages

Showing posts with label multi threading. Show all posts
Showing posts with label multi threading. Show all posts

Saturday, February 20, 2016

Async and Await Keyword in C#

Async and Await keywords are used together to run a method asynchronously(not multi-threaded).  An async method will be run synchronously if it does not contain an await keyword. An async method either returns void or a task.

With these keywords, we run methods in an asynchronous way. Threads are optional. This style of code is more responsive. A network access can occur with no program freeze.

Eg: In the following example, the task run asynchronously. Thus, the two console.writeline statements are executed before the task completes.

using System;
using System.Threading.Tasks;
using System.Threading;
internal class Program
{
    private static void Main(string[] args)
    {
        var task = DoWork();
        Console.WriteLine("Task status: " + task.Status);
        Console.WriteLine("Waiting for ENTER");
        Console.ReadLine();
    }

    private static async Task DoWork()
    {
        Console.WriteLine("Entered DoWork(). Sleeping 3");
        // imitating time consuming code
        // in a real-world app this should be inside task, 
        // so method returns fast
        Thread.Sleep(3000);

        await Task.Run(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine("async task iteration " + i);
                    // imitating time consuming code
                    Thread.Sleep(1000);
                }
            });

        Console.WriteLine("Exiting DoWork()");
    }
}
Output:
Entered DoWork(). Sleeping 3
async task iteration 0
Task status: WaitingForActivation
Waiting for ENTER
async task iteration 1
async task iteration 2
async task iteration 3
async task iteration 4
async task iteration 5
async task iteration 6
async task iteration 7
async task iteration 8
async task iteration 9
Exiting DoWork()

Friday, February 19, 2016

Setting Up Multithreading in PHP

Multi-threading in PHP is a very useful feature to take advantage of multi-core processors that is common today. However, multi-threading is not enabled by default.

To set up multi-threading in PHP, follow the following steps.

1. Check the PHP Extension Build for the php version you are using:

 After you have set up your website, go to the link:

localhost:8080/?phpinfo=1

The port number depends on the port you have used, it may be default 80 or any other port number
Look for PHP Extension Build in that page. Check the VC number. For me, its VC11. VC stands for Visual C++, the compiler version that was used to build the version of PHP that you are using. It is interesting to note that PHP which is itself a programming language is built using C and C++. Thus, a compiler need to be used to compile C code.

2. Go to http://windows.php.net/downloads/pecl/releases/pthreads/:

Click on the link above and select and download the latest version of zip file that has the same VC number as that of php version that you are using. For VC11, its 2.0.9. I downloaded php_pthreads-2.0.9-5.6-ts-vc11-x64.zip as I am using a 64 bit computer.

3. Extract the zip.

Move php_pthreads.dll to the php\ext\ directory.
Move pthreadVC2.dll to the php\ directory.

4. Change php.ini file

Open php\php.ini and add

extension=php_pthreads.dll

5. Restart your web server