使用双线程的c语言代码,多线程的使用

[c]代码库#include

#include

#include

struct Achar{

int x;

int y;

int speed;

char a;

};

int stop=1;

pthread_t t[26];

pthread_t tid;

pthread_mutex_t m;//互斥量

struct Achar a[26];

void *run(void *d)

{

int id;

static idx=-1;

idx++;

id=idx;

while(stop)

{

pthread_mutex_lock(&m);

a[id].y+=a[id].speed;

if(a[id].y>=LINES)

{

a[id].y=rand()%(LINES/4);

}

pthread_mutex_unlock(&m);

sched_yield();

usleep(100000);

}

}

void *update(void *d)

{

int i;

while(1)

{

erase();

for(i=0;i<26;i++)

{

mvaddch(a[i].y,a[i].x,a[i].a);

}

refresh();

usleep(10000);

}

}

main()

{

int i;

initscr();

curs_set(0);

noecho();

keypad(stdscr,TRUE);

for(i=0;i<26;i++)

{

a[i].x=rand()%COLS;

a[i].y=rand()%LINES;

//a[i].speed=1+rand()%10;

a[i].speed=1;

a[i].a='*';

}

pthread_mutex_init(&m,0);

pthread_create(&tid,0,update,0);

for(i=0;i<26;i++)

{

pthread_create(&t[i],0,run,0);

}

getch();

stop=0;

for(i=0;i<26;i++)

{

pthread_join(t[i],(void **)0);

}

pthread_join(tid,(void **)0);

pthread_mutex_destroy(&m);

endwin();

}

694748ed64b9390909c0d88230893790.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值