① c语言如何实现多线程同时运行
1、点击菜单栏的“Project”选项卡,下拉列表的最后一项“Project options...”是对当前工程的的属性进行设置的。
② c语言中如何创建新的线程
进程的生命周期:[1].创建 --- fork [2].执行 --- a. execb.子进程实现代码逻辑[3].结束 --- exit _exit僵尸态进程---wait waitpid孤儿进程--------------------------------------进程存在的问题:(1).进程的创建 --- 复制(时间 和 空间的开销很大)(2).进程的运行 --- 调度-->
pthread_create创建一个线程,thread是用来表明创建线程的ID,attr指出线程创建时候的属性,我们用NULL来表明使用缺省属性。start_routine函数指针是线程创建成功后开始执行的函数,arg是这个函数的唯一一个参数。表明传递给start_routine的参数。
pthread_exit函数和exit函数类似用来退出线程.这个函数结束线程,释放函数的资源,并在最后阻塞,直到其他线程使用pthread_join函数等待它。然后将*retval的值传递给**thread_return.由于这个函数释放所以的函数资源,所以retval不能够指向函数的局部变量。
pthread_join和wait调用一样用来等待指定的线程。下面我们使用一个实例来解释一下使用方法.在实践中,我们经常要备份一些文件。下面这个程序可以实现当前目录下的所有文件备份。
③ 面试被问到,创建线程有几种方式
创建线程有三种方式:
①继承Thread类(真正意义上的线程类),是Runnable接口的实现。
②实现Runnable接口,并重写里面的run方法。
③使用Executor框架创建线程池。Executor框架是juc里提供的线程池的实现。
④ C语言如何创建多线程
多线程是操作系统提供的系统操作,不是C语言的概念。
多线程和你的操作系统有关,和C语言关系不大。
windows和Linux的多线程接口不一样。
⑤ C语言怎么写线程代码
通常使用CreateThread函数来创建新的线程.(Unix下使用pthread_create函数)
首先指出,线程与线程之间,是并列关系,不会存在"父子线程"的概念.
在Windows平台下,CreateThread函数包含在 Windows.h 文件内,包含此文件即可正常使用.
以下为CreateThread函数的声明:
HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes,//指向安全性属性描述结构体的
//指针,通常可以忽略的.
SIZE_T dwStackSize,//指定新线程初始的栈大小,若不关心,可以用0填充,来要求使用
//默认值
LPTHREAD_START_ROUTINE lpStartAddress,//用来充当线程的函数的指针.
LPVOID lpParameter,//要传递给函数的参数,这个值本身就是那个参数,而不是参数的地址
DWORD dwCreationFlags,//创建的方式,0表示正常,创建后立即开始运行
LPDWORD lpThreadId//用来接受函数反馈的线程ID的指针.
);
用来充当新的线程的函数格式:
DWORD WINAPI ThreadProc(LPVOID);
CreateThread函数若成功了,返回新线程的句柄,若失败了,则返回NULL.
若用CREATE_SUSPENDED填充dwCreation Flags则创建的线程先挂起来,并不直接开始运行,要用ResumeThread函数恢复线程,才能继续运行.
⑥ C语言多线程的操作步骤
线程创建
函数原型:intpthread_create(pthread_t*restrict tidp,const pthread_attr_t *restrict attr,void *(*start_rtn)(void),void *restrict arg);
返回值:若是成功建立线程返回0,否则返回错误的编号。
形式参数:pthread_t*restrict tidp要创建的线程的线程id指针;const pthread_attr_t *restrict attr创建线程时的线程属性;void *(start_rtn)(void)返回值是void类型的指针函数;void *restrict arg start_rtn的形参。
线程挂起:该函数的作用使得当前线程挂起,等待另一个线程返回才继续执行。也就是说当程序运行到这个地方时,程序会先停止,然后等线程id为thread的这个线程返回,然后程序才会断续执行。
函数原型:intpthread_join(pthread_tthread, void **value_ptr);
参数说明如下:thread等待退出线程的线程号;value_ptr退出线程的返回值。
返回值:若成功,则返回0;若失败,则返回错误号。
线程退出
函数原型:voidpthread_exit(void *rval_ptr);
获取当前线程id
函数原型:pthread_tpthread_self(void);
互斥锁
创建pthread_mutex_init;销毁pthread_mutex_destroy;加锁pthread_mutex_lock;解锁pthread_mutex_unlock。
条件锁
创建pthread_cond_init;销毁pthread_cond_destroy;触发pthread_cond_signal;广播pthread_cond_broadcast;等待pthread_cond_wait。
⑦ C语言中 怎么实现双线程 或者 父子线程啊
运行一个程序,这个运行实体就是一个“进程”。
例如,用鼠标双击IE浏览器的图标,你运行了一个IE“进程”。第一个窗未关,你又用鼠标双击IE浏览器的图标,又出来一个浏览器的窗。这时,你运行了同一个程序的两个进程。
对于自己写的程序也如此。运行它,这个运行实体就是一个“进程”。同时运行两个,就是两个进程。计算机分别对两个进程分配资源,直到进程结束,收回资源。
线程是进程里真真跑的线路,真真执行的运算,每个进程有一个主线程。进程里可以开第二第三条新的执行线路,gcc 用 pthread_create(),VC++ 用 CreateThread(), 这就叫双线程和多线程。进程是线程的容器,同一进程的线程共享它们进程的资源。线程里建的线程就是父子线程。
两个或多个进程协同工作时,需要互相交换信息,有些情况下进程间交换的少量信息,有些情况下进程间交换大批信息。这就要通讯。通讯方式不止一种。管道就是一种。VC++ 用 CreatePipe() 函数建立。
管道的实质是一个共享文件,可借助于文件系统的机制实现,创建、打开、关闭和读写.
一个进程正在使用某个管道写入或读出数据时,另一个进程就必须等待. 发送者和接收者双方必须知道对方是否存在,如果对方已经不存在,就没有必要再发送信息.,发送信息和接收信息之间要协调,当写进程把一定数量的数据写入管道,就去睡眠等待,直到读进程取走数据后,把它唤醒。
VC++ 线程例子:
#include <windows.h>
#include <iostream.h>
DWORD WINAPI fun1(LPVOID lp);
DWORD WINAPI fun2(LPVOID lp);
int piao=500;
int main()
{
HANDLE pthread1,pthread2;
pthread1=CreateThread(0,0,fun1,0,0,0);
pthread2=CreateThread(0,0,fun2,0,0,0);
CloseHandle(pthread1);
CloseHandle(pthread2);
Sleep(3000);
return 0;
}
DWORD WINAPI fun1(LPVOID lp)
{
while(1)
{
if(piao>0)
cout<< "thread-1-"<< piao--<<endl;
else
break;
}
return 0;
}
DWORD WINAPI fun2(LPVOID lp)
{
while(1)
{
if(piao>0)
cout<<"thread-2-"<<piao--<<endl;
else
break;
}
return 0;
}
===================================
建管道函数原形:
BOOL CreatePipe(
PHANDLE hReadPipe, // read handle
PHANDLE hWritePipe, // write handle
LPSECURITY_ATTRIBUTES lpPipeAttributes, // security attributes
DWORD nSize // pipe size
);
⑧ c语言中怎样创建多线程
/*这是我写的最简单的多线程程序,看懂不?*/
#include <windows.h>
#include <stdio.h>
//#include <strsafe.h>
DWORD WINAPI ThreadProc1( LPVOID lpParam )
{
int i=0,j=0;
while(1)
{
printf("hello,this thread 1 ...\n");
//延时
for(i=0;i<200000000;i++)
{
;
}
}
}
DWORD WINAPI ThreadProc2( LPVOID lpParam )
{
int i=0,j=0;
while(1)
{
printf("hello,this thread 2 ...\n");
//延时
for(i=0;i<200000000;i++)
{
;
}
}
}
void main()
{
int i=0;
//创建线程1
CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadProc1, // thread function
NULL, // argument to thread function
0, // use default creation flags
NULL); // returns the thread identifier
//创建线程2
CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadProc2, // thread function
NULL, // argument to thread function
0, // use default creation flags
NULL); // returns the thread identifier
//让主线程进入循环,主线程若退出,子线程1,2会被系统“杀死”
while(1)
{
printf("hello,this thread 0 ...\n");
//延时
for(i=0;i<200000000;i++)
{;}
}
}
⑨ 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语言如何创建线程(windows)系统中
下面为C语言调用WIN API实现创建线程:
1,导入<windows.h>头文件
2,声明实现方法DWORD WINAPI ThreadProc1( LPVOID lpParam ) {}
3,在main()方法中调用 CreateThread(NULL,0 ,ThreadProc1,NULL,0,NULL);
要注意的是主线程不能结束,如果主线程结束,则它的子线程也会被杀死。
#include <windows.h>
#include <stdio.h>
#include<time.h>
DWORD WINAPI ThreadProc1( LPVOID lpParam )
{
int i=0;
time_t timer;
while(1)
{
timer=time(NULL);
printf("The current time is: %s\n",asctime(localtime(&timer)));
sleep(1);
}
}
void main()
{
int i=0;
//让主线程进入循环,主线程若退出,子线程1,2会被系统“杀死”
//创建线程1
CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadProc1, // thread function
NULL, // argument to thread function
0, // use default creation flags
NULL); // returns the thread identifier
for(;;)
{
;
}
}