首先是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*’ to ‘const 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这个版本的多线程显然有问题,待查原因。