VC下的C语言多线程

代码:

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


void* tprocess1(void* args){
       int i=1;
       while(i<=10){
            printf("process1:%d\n",i);
            i++;
       }
       return NULL;
}

void* tprocess2(void* args){
       int i=1;
       while(i<=10){
            printf("process2:%d\n",i);
            i++;
       }
       return NULL;
}

int main(){
       pthread_t t1;
       pthread_t t2;
       pthread_create(&t1,NULL,tprocess1,NULL);
       pthread_create(&t2,NULL,tprocess2,NULL);
       pthread_join(t1,NULL);
       pthread_join(t2,NULL);
       return 0;
}

 

运行之前需要做一些配置:

1.下载PTHREAD的WINDOWS开发包 pthreads-w32-2-4-0-release.exe(任何一个版本均可)

  http://sourceware.org/pthreads-win32/ ,解压到一个目录。

2.找到include和lib文件夹,下面分别把它们添加到VC++6.0的头文件路径和静态链接库路径下面:

  a).Tools->Options,选择Directory页面,然后在Show directories for:中选择Include files(默认),

     在Directories中添加include的路径。在Show directories for:中选择Library files,

     在Directories中添加lib的路径。 

  b).Project->Settings,选择Link页面,然后将lib下的*.lib文件添加到Object/library Modules,

     各lib文件以空格隔开。 

  c).将lib下的*.dll文件复制到工程目录下,即根目录。  

 

如果不配置环境,将出现以下错误:

Linking...
工程名.obj : error LNK2001: unresolved external symbol __imp__pthread_join
工程名.obj : error LNK2001: unresolved external symbol __imp__pthread_create
Debug/工程名.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.

工程名.exe - 3 error(s), 0 warning(s)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值