0
点赞
收藏
分享

微信扫一扫

c语言多线程(c语言编译器)

c语言为什么叫C“语言?
C语言是1972年由美国的DennisRitchie设计发明的,并首次在UNIX操作系统的DECPDP-11计算机上使用。它由早期的编程语言BCPL(BasicCombindProgrammingLanguage)发展演变而来。在1970年,AT&T贝尔实验室的KenThompson根据BCPL语言设计出较先进的并取名为B的语言,最后导了C语言的问世。
什么是C语言
C语言是一种计算机程序设计语言。它既有高级语言的特点,又具有汇编语言的特点。它可以作为系统设计语言,编写工作系统应用程序,也可以作为应用程序设计语言,编写不依赖计算机硬件的应用程序。因此,它的应用范围广泛。
C语言多线程的概念
线程:线程是程序中的一个执行流,每个线程都有自己的专有寄存器(栈指针、程序计数器等),但代码区是共享的,即不同的线程可以执行同样的函数。
多线程:多线程是指程序中包含多个执行流,即在一个程序中可以同时运行多个不同的线程来执行不同的任务,也就是说允许单个程序创建多个并行执行的线程来完成各自的任务。
C语言的开始设计,并未设计多线程的机制,由于随着软硬件的发展及需求的发展。后来C语言才开发了线程库以支持多线程的操作、应用。
主要基于Linux介绍C多线程。在编译C的多线程时候,一方面必须指定Linux C语言线程库多线程库pthread,才可以正确编译(例如:gcc test.c -o test -lpthread);另一方面要包含有关线程头文件#include



c语言如何编写一个简单的多线程程序
这是一个多线程例子,里面只有两个线程,是生产者/消费者模式,已编译通过,注释很详细,
/* 以生产者和消费者模型问题来阐述Linux线程的控制和通信你
生产者线程将生产的产品送入缓冲区,消费者线程则从中取出产品。
缓冲区有N个,是一个环形的缓冲池。
*/
#include
#include
#define BUFFER_SIZE 16
struct prodcons
int buffer[BUFFER_SIZE];/*实际存放数据的数组*/
pthread_mutex_t lock;/*互斥体lock,用于对缓冲区的互斥操作*/
int readpos,writepos; /*读写指针*/
pthread_cond_t notempty;/*缓冲区非空的条件变量*/
pthread_cond_t notfull;/*缓冲区未满 的条件变量*/
/*初始化缓冲区*/
void pthread_init( struct prodcons *p)
pthread_mutex_init(&p->lock,NULL);
pthread_cond_init(&p->notempty,NULL);
pthread_cond_init(&p->notfull,NULL);
p->readpos = 0;
p->writepos = 0;
/*将产品放入缓冲区,这里是存入一个整数*/
void put(struct prodcons *p,int data)
pthread_mutex_lock(&p->lock);
/*等待缓冲区未满*/
if((p->writepos +1)%BUFFER_SIZE ==p->readpos)
{
pthread_cond_wait(&p->notfull,&p->lock);
}
p->buffer[p->writepos] =data;
p->writepos++;
if(p->writepos >= BUFFER_SIZE)
p->writepos = 0;
pthread_cond_signal(&p->notempty);
pthread_mutex_unlock(&p->lock);
/*从缓冲区取出整数*/
int get(struct prodcons *p)
int data;
pthread_mutex_lock(&p->lock);
/*等待缓冲区非空*/
if(p->writepos == p->readpos)
{
pthread_cond_wait(&p->notempty ,&p->lock);//非空就设置条件变量notempty
}
/*读书据,移动读指针*/
data = p->buffer[p->readpos];
p->readpos++;
if(p->readpos == BUFFER_SIZE)
p->readpos = 0;
/*设置缓冲区未满的条件变量*/
pthread_cond_signal(&p->notfull);
pthread_mutex_unlock(&p->lock);
return data;
/*测试:生产站线程将1 到1000的整数送入缓冲区,消费者线程从缓冲区中获取整数,两者都打印信息*/
#define OVER (-1)
struct prodcons buffer;
void *producer(void *data)
int n;
for( n=0;n<1000;n++)
{
printf("%d ------>\n",n);
put(&buffer,n);
}
put(&buffer,OVER);
return NULL;
void *consumer(void *data)
int d;
while(1)
{
d = get(&buffer);
if(d == OVER)
break;
else
printf("----->%d\n",d);
}
return NULL;
int main()
pthread_t th_p,th_c;
void *retval;
pthread_init(&buffer);
pthread_create(&th_p,NULL,producer,0);
pthread_create(&th_c,NULL,consumer,0);
/*等待两个线程结束*/
pthread_join(th_p, &retval);
pthread_join(th_c,&retval);
return 0;
#include

#include

#include

handle thread1;
handle thread2;
int fh=0,jd1=0,jd2=0;
dword winapi fun1(lpvoid lpparameter)
int s=(int)lpparameter;
int i;
for(i=2;i
if(s%i==0)break;
if(i
fh=1;
closehandle(thread2);
jd2=1;
jd1=1;
return 0;
dword winapi fun2(lpvoid lpparameter)
int s=(int)lpparameter;//参数传递方式
int i;
for(i=s/2;i
if(s%i==0)break;
if(i
fh=1;
closehandle(thread1); //结束线程
jd1=1;
jd2=1;
return 0;
main()
int s=113;
thread1=createthread(null,0,fun1,(lpvoid)s,0,null);
thread2=createthread(null,0,fun2,(lpvoid)s,0,null);
while(jd1==0 || jd2==0){};//主线程等待
if(fh==1)
printf("%d是合数\n",s);
else
printf("%d是质数\n",s);
system("pause>nul");
}#include
#include
#include
#include
using namespace std;
DWORD WINAPI Function1Proc(LPVOID lpParameter);
DWORD WINAPI Function2Proc(LPVOID lpParameter);
bool stop=false;
int main()
HANDLE hThread1;
HANDLE hThread2;
hThread1=CreateThread(NULL,0,Function1Proc,NULL,0,NULL);
hThread2=CreateThread(NULL,0,Function2Proc,NULL,0,NULL);
if (getchar())stop=true;
WaitForSingleObject(hThread1,INFINITE);
WaitForSingleObject(hThread2,INFINITE);
return 0;
DWORD WINAPI Function1Proc(LPVOID lpParameter)
while(!stop)
cout<
查看追问
追问:
非常感谢你的细致回答,但是我没学过C++,抱歉
2014-02-19
0

举报

相关推荐

0 条评论