c语言线程函数6 c语言线程创建函数

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语言 怎么定义一个多线程函数呢,急等main()
{
if(!fork())
{
//代码
//...新线程,与原线程共享数据空间
}
else
{
//代码
//..原线程,与新线程共享数据空间
}
}
这样就可以了
但是vc不可以用的
vc有自己的c++多线程函数
c语言怎么创建线程和使用1、添加线程相关的头文件:#includepthread.h
2、线程创建函数是pthread_create()函数,该函数的原型为:
int pthread_create(pthread_t *thread,pthread_attr_t *attr,void* (*start_routine)(void*),void *arg);
3、线程退出函数是pthread_exit()函数,该函数的原型为:
void pthread_exit(void *retval);
创建线程的示例程序如下:
/*
**程序说明:创建线程函数pthread_create()函数的使用 。
*/
#include stdio.h
#include pthread.h
#include unistd.h
#include stdlib.h
#include string.h
//打印标识符的函数
void print_ids(const char *str)
{
pid_t pid; //进程标识符
pthread_t tid; //线程标识符
pid=getpid(); //获得进程号
tid=pthread_self(); //获得线程号
printf("%s pid:%u tid:%u (0x%x)\n",
str,(unsigned int)pid,(unsigned int)tid,(unsigned int)tid); //打印进程号和线程号
}
//线程函数
void* pthread_func(void *arg)
{
print_ids("new thread:"); //打印新建线程号
return ((void*)0);
}
//主函数
int main()
{
int err;
pthread_t ntid; //线程号
err=pthread_create(ntid,NULL,pthread_func,NULL); //创建一个线程
if(err != 0)
{
printf("create thread failed:%s\n",strerror(err));
exit(-1);
}
print_ids("main thread:"); //打印主线程号
sleep(2);
return 0;
}
C语言多线程控制函数this.Invoke((EventHandler)(delegate
{
READ1();
}));
this.Invoke((EventHandler)(delegate
{
READ2();
}));
C语言线程函数参数问题·线程创建
函数原型:int pthread_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的形参 。=====这个地方就可以传参数,

推荐阅读