當前位置:首頁 » 編程語言 » c語言中線程間通信
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言中線程間通信

發布時間: 2022-06-13 05:49:19

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變數,那麼在任何一個線程內都是可見的,其實這也是線程比進程間通信更加方便的原因。