Ⅰ c语言如何实现多线程同时运行
1、点击菜单栏的“Project”选项卡,下拉列表的最后一项“Project options...”是对当前工程的的属性进行设置的。
Ⅱ 线程之间的通信例子 求一个WINDOWS下多线程间通信的例子,用C语言编写!
#include
<stdio.h>
int
main(int
argc,
char
**argv){
CreateThread(NULL,
0,
thread2,
this,
0,
0);
printf("主线程正在执行!\n");
return
0;
}
void
thread2(){
sleep(2);//睡2毫秒
printf("第二个线程在运行!\n");
}
这个例子可能很简单,但能说明问题了。
Ⅲ c语言实例,linux线程同步的信号量方式 谢谢
这么高的悬赏,实例放后面。信号量(sem),如同进程一样,线程也可以通过信号量来实现通信,虽然是轻量级的。信号量函数的名字都以"sem_"打头。线程使用的基本信号量函数有四个。
信号量初始化。
intsem_init(sem_t*sem,intpshared,unsignedintvalue);
这是对由sem指定的信号量进行初始化,设置好它的共享选项(linux只支持为0,即表示它是当前进程的局部信号量),然后给它一个初始值VALUE。
等待信号量。给信号量减1,然后等待直到信号量的值大于0。
intsem_wait(sem_t*sem);
释放信号量。信号量值加1。并通知其他等待线程。
intsem_post(sem_t*sem);
销毁信号量。我们用完信号量后都它进行清理。归还占有的一切资源。
intsem_destroy(sem_t*sem);
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>
#include<errno.h>
#definereturn_if_fail(p)if((p)==0){printf("[%s]:funcerror!/n",__func__);return;}
typedefstruct_PrivInfo
{
sem_ts1;
sem_ts2;
time_tend_time;
}PrivInfo;
staticvoidinfo_init(PrivInfo*thiz);
staticvoidinfo_destroy(PrivInfo*thiz);
staticvoid*pthread_func_1(PrivInfo*thiz);
staticvoid*pthread_func_2(PrivInfo*thiz);
intmain(intargc,char**argv)
{
pthread_tpt_1=0;
pthread_tpt_2=0;
intret=0;
PrivInfo*thiz=NULL;
thiz=(PrivInfo*)malloc(sizeof(PrivInfo));
if(thiz==NULL)
{
printf("[%s]:Failedtomallocpriv./n");
return-1;
}
info_init(thiz);
ret=pthread_create(&pt_1,NULL,(void*)pthread_func_1,thiz);
if(ret!=0)
{
perror("pthread_1_create:");
}
ret=pthread_create(&pt_2,NULL,(void*)pthread_func_2,thiz);
if(ret!=0)
{
perror("pthread_2_create:");
}
pthread_join(pt_1,NULL);
pthread_join(pt_2,NULL);
info_destroy(thiz);
return0;
}
staticvoidinfo_init(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
thiz->end_time=time(NULL)+10;
sem_init(&thiz->s1,0,1);
sem_init(&thiz->s2,0,0);
return;
}
staticvoidinfo_destroy(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
sem_destroy(&thiz->s1);
sem_destroy(&thiz->s2);
free(thiz);
thiz=NULL;
return;
}
staticvoid*pthread_func_1(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
while(time(NULL)<thiz->end_time)
{
sem_wait(&thiz->s2);
printf("pthread1:pthread1getthelock./n");
sem_post(&thiz->s1);
printf("pthread1:pthread1unlock/n");
sleep(1);
}
return;
}
staticvoid*pthread_func_2(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
while(time(NULL)<thiz->end_time)
{
sem_wait(&thiz->s1);
printf("pthread2:pthread2gettheunlock./n");
sem_post(&thiz->s2);
printf("pthread2:pthread2unlock./n");
sleep(1);
}
return;
}
Ⅳ 有windows上C语言开发多线程socket通信的例子么
http://www.jb51.net/article/44156.htm
这里的OK不?
Ⅳ c语言多线程如何运行指定时间
第一步: 将源文件1(1.c)修改为如下形式: #include "print.h" #include "2.c" int main(void) { printHello(); return 0; } 其中的2.c就是源文件2的文件名 第二步,将三个文件保存到同一目录中 第三步,打开TC2,执行FILE-CHANGE DIR,将工作目录换到三个文件所在的目录。 第四步,在TC2中打开1.c文件,编译运行。 建议不要再使用TC2这个相对原始的IDE了,上面介绍的这个方法也并不是标准方法,建议使用TC2006/VC/BCB等现代的IDE环境,如果实在是舍弃不下DOS字符界面,那就试试GCC吧!
Ⅵ 贪食蛇游戏,c语言怎么实现多线程
使用pthread库执行多线程,这个是Linux下的线程库 Windows下应该有自己的API,不过这种东西一般还是以Linux为标准。pthread_create()创建一个线程,传入fun()的函数指针就行了。
然后这个Beep()的需求要进行线程间通信,可以用共享内存的方法,设一个bool变量flag共享,然后beep的时候设为false,beep完设成true。fun()里面每次看一下这个flag,是false的话就不做动作等下一秒,基本可以满足需求。
这样做的好处是实现简单,但时间是以1s为单位的。如果要8秒结束立刻执行,需要用条件变量的方法来控制,比较复杂,这样的实现方式一个可以满足需求了。
Ⅶ c语言多线程的socket简单模板,想参考一下写一个简单的程序
中国络的Socket数据传输是一种特殊的I/O,Socket也是一种文件描述符。Socket也具有一个类似于打开文件的函数调用Socket(),该函数返回一个整型的Socket描述符,随后的连接建立、数据传输等操作都是通过该Socket实现的。 下面用Socket实现一个windows下的c语言socket通信例子,这里我们客户端传递一个字符串,服务器端进行接收。 【服务器端】 #include "stdafx.h" #include #include #include #define SERVER_PORT 5贰0吧 //侦听端口 void main(
Ⅷ c语言如何编写一个简单的多线程程序
这是一个多线程例子,里面只有两个线程,是生产者/消费者模式,已编译通过,注释很详细,
如下:
/* 以生产者和消费者模型问题来阐述Linux线程的控制和通信你
生产者线程将生产的产品送入缓冲区,消费者线程则从中取出产品。
缓冲区有N个,是一个环形的缓冲池。
*/
#include <stdio.h>
#include <pthread.h>
#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 *procer(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,procer,0);
pthread_create(&th_c,NULL,consumer,0);
/*等待两个线程结束*/
pthread_join(th_p, &retval);
pthread_join(th_c,&retval);
return 0;
}
Ⅸ 用C语言如何实现多线程同时运行的情况下,各个线程输出不同的随机数
1、使用pthread库执行多线程,这个是Linux下的线程库 Windows下应该有自己的API,不过这种东西一般还是以Linux为标准。pthread_create()创建一个线程,传入fun()的函数指针就行了。然后这个Beep()的需求要进行线程间通信,可以用共享内存的方法,设一个bool变量flag共享,然后beep的时候设为false,beep完设成true。fun()里面每次看一下这个flag,是false的话就不做动作等下一秒,基本可以满足需求。
2、例程:
#include<pthread.h>
#include<stdio.h>
#include<sys/time.h>
#include<string.h>
#defineMAX10
pthread_tthread[2];
pthread_mutex_tmut;
intnumber=0,i;
void*thread1()
{
printf("thread1:I'mthread1 ");
for(i=0;i<MAX;i++)
{
printf("thread1:number=%d ",number);
pthread_mutex_lock(&mut);
number++;
pthread_mutex_unlock(&mut);
sleep(2);
}
printf("thread1:主函数在等我完成任务吗? ");
pthread_exit(NULL);
}
void*thread2()
{
printf("thread2:I'mthread2 ");
for(i=0;i<MAX;i++)
{
printf("thread2:number=%d ",number);
pthread_mutex_lock(&mut);
number++;
pthread_mutex_unlock(&mut);
sleep(3);
}
printf("thread2:主函数在等我完成任务吗? ");
pthread_exit(NULL);
}
voidthread_create(void)
{
inttemp;
memset(&thread,0,sizeof(thread));//comment1
/*创建线程*/
if((temp=pthread_create(&thread[0],NULL,thread1,NULL))!=0)//comment2
printf("线程1创建失败! ");
else
printf("线程1被创建 ");
if((temp=pthread_create(&thread[1],NULL,thread2,NULL))!=0)//comment3
printf("线程2创建失败");
else
printf("线程2被创建 ");
}
voidthread_wait(void)
{
/*等待线程结束*/
if(thread[0]!=0){//comment4
pthread_join(thread[0],NULL);
printf("线程1已经结束 ");
}
if(thread[1]!=0){//comment5
pthread_join(thread[1],NULL);
printf("线程2已经结束 ");
}
}
intmain()
{
/*用默认属性初始化互斥锁*/
pthread_mutex_init(&mut,NULL);
printf("我是主函数哦,我正在创建线程,呵呵 ");
thread_create();
printf("我是主函数哦,我正在等待线程完成任务阿,呵呵 ");
thread_wait();
return0;
}
Ⅹ 线程之间如何共享socket用C语言
你的意思是线程之间共用一个socket吗,如果是,那比较容易,在主进程内定义一个socket变量,那么在任何一个线程内都是可见的,其实这也是线程比进程间通信更加方便的原因。