[心得]3种编程环境下多线程编程示例

首先是pthread:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

int sum;
void *runner(void *param);/*the thread*/

int main(int argc, char *argv[])
{
    pthread_t tid;
    pthread_attr_t attr;

    if(argc!=2)
    {
        fprintf(stderr, "usage: a.out <Int val>\n");
        return -1;
    }

    if(atoi(argv[1])<0)
    {
        fprintf(stderr, "%d should > 0\n", atoi(argv[1]));
        return -1;
    }

    pthread_attr_init(&attr);
    pthread_create(&tid, &attr, runner, argv[1]);

    pthread_join(tid, NULL);

    printf("sum=%d\n",sum);
}

/*The thread will begin control in this function*/
void *runner(void *param)
{
    int i;
    int upper = atoi(param);
    sum = 0;

    for(i=0;i<=upper;i++)
    {
        sum +=i;
    }

    pthread_exit(0);
}

注意编译时使用gcc:

$ gcc -lpthread test_pthread.c
$ #if use g++
$ g++ -lpthread test_pthread.c
test_pthread.c: In function ‘void* runner(void*)’:
test_pthread.c:37:27: error: invalid conversion from ‘void*’ toconst char*’ [-fpermissive]
In file included from test_pthread.c:3:0:
/usr/include/stdlib.h:148:12: error:   initializing argument 1 of ‘int atoi(const char*)’ [-fpermissive]

其次是win32,

#include <windows.h>
#include <stdio.h>

DWORD Sum; /*shared by the threads*/
DWORD WINAPI Summation(LPVOID Param)
{
    DWORD Upper = *(DWORD*)Param;
    for(DWORD i=0;i<=Upper;i++)
    {
        Sum+=i;
    }
    return 0;
}

int main(int argc, char *argv[])
{
    DWORD ThreadId;
    HANDLE ThreadHandle;
    int Param;

    if(argc!=2)
    {
        fprintf(stderr, "An Int param is required!\n");
        return -1;
    }
    Param = atoi(argv[1]);
    if(Param<0)
    {
        fprintf(stderr, "An Int>=0 is required!\n");
        return -1;
    }

    ThreadHandle = CreateThread(
        NULL,//default security attributes
        0,//default stack size
        Summation,//thread function
        &Param,//parameter to thread function
        0,//default creation flags
        &ThreadId);//returns the thread identifier

    if(ThreadHandle != NULL)
    {
        //wait for the thread to finish
        WaitForSingleObject(ThreadHandle, INFINITE);

        //close the thread handle
        CloseHandle(ThreadHandle);

        printf("sum=%d\n", Sum);
    }
}

这里使用VS2010编译时需要注意带参debug:
在右侧工程导览窗口右键,选择属性,再到debug栏,设置debug参数即可。

class Sum
{
    private int sum;
    public int getSum()
    {
        return sum;
    }

    public void setSum(int value)
    {
        this.sum = sum;
    }
}

class Summation implements Runnable
{
    private int upper;
    private Sum sumValue;

    public Summation(int upper, Sum sumValue)
    {
        this.upper = upper;
        this.sumValue = sumValue;
    }

    public void run()
    {
        int sum = 0;
        for(int i = 0;i<=upper;i++)
        {
            sum += i;
        }

        sumValue.setSum(sum);
    }
}

public class Driver
{
    public static void main(String[] args)
    {
        if(args.length>0)
        {
            if(Integer.parseInt(args[0])<0)
            {
                System.err.println(args[0] + "must > 0");
            }else{
                Sum sum = new Sum();
                int upper = Integer.parseInt(args[0]);
                Thread thrd = new Thread(new Summation(upper, sum));
                thrd.start();
                try{
                    thrd.join();
                    System.out.println("The sum of " + upper+ " is "+sum.getSum());
                }catch(InterruptedException ie){
                    System.err.println("cathc InterruptedException");
                }
            }
        }else{
            System.err.println("Usage:Summation <Int>");
        }
    }
}

使用java 编译类时,首先要求类的名字必须取名为Drive.java

test_thread.java:38: error: class Driver is public, should be declared in a file named Driver.java
public class Driver
       ^

其次,

$ java Driver
Error: Could not find or load main class Driver

根据这个提示:
http://stackoverflow.com/questions/7485670/error-could-not-find-or-load-main-class
猜测是Java环境没配好,检查后发现,classpath中少配了当前目录。

$ javac Driver.java
$ java Driver
The sum of 8 is 0

java这个版本的多线程显然有问题,待查原因。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值