首页 > 编程知识 正文

c语言线程设计,c++线程编程

时间:2023-12-29 20:31:51 阅读:330785 作者:YHDM

本文目录一览:

c语言多线程编程问题

C语言中多线程的实现原理就是线程的原理,本人只了解Linux下面的C,linux下面的线程,不就是进程中的一个控制流么,相对来说代码很简单,但是原理却是很复杂,很难说清,还需要自己详细学习研究,下面是一个很简单的例子,哪边都能找到,自己运行下看看吧

#include pthread.h

#include stdio.h

#include sys/time.h

#include string.h

#define MAX 10

pthread_t thread[2];

pthread_mutex_t mut;

int number=0, i;

void *thread1()

{

printf ("thread1 : I'm thread 1n");

for (i = 0; i MAX; i++)

{

printf("thread1 : number = %dn",number);

pthread_mutex_lock(mut);

number++;

pthread_mutex_unlock(mut);

sleep(2);

}

printf("thread1 :主函数在等我完成任务吗?n");

pthread_exit(NULL);

}

void *thread2()

{

printf("thread2 : I'm thread 2n");

for (i = 0; i MAX; i++)

{

printf("thread2 : number = %dn",number);

pthread_mutex_lock(mut);

number++;

pthread_mutex_unlock(mut);

sleep(3);

}

printf("thread2 :主函数在等我完成任务吗?n");

pthread_exit(NULL);

}

void thread_create(void)

{

int temp;

memset(thread, 0, sizeof(thread)); //comment1

/*创建线程*/

if((temp = pthread_create(thread[0], NULL, thread1, NULL)) != 0) //comment2

printf("线程1创建失败!n");

else

printf("线程1被创建n");

if((temp = pthread_create(thread[1], NULL, thread2, NULL)) != 0) //comment3

printf("线程2创建失败");

else

printf("线程2被创建n");

}

void thread_wait(void)

{

/*等待线程结束*/

if(thread[0] !=0) { //comment4

pthread_join(thread[0],NULL);

printf("线程1已经结束n");

}

if(thread[1] !=0) { //comment5

pthread_join(thread[1],NULL);

printf("线程2已经结束n");

}

}

int main()

{

/*用默认属性初始化互斥锁*/

pthread_mutex_init(mut,NULL);

printf("我是主函数哦,我正在创建线程,呵呵n");

thread_create();

printf("我是主函数哦,我正在等待线程完成任务阿,呵呵n");

thread_wait();

return 0;

}

C语言多线程实现

多线程随机选号程序

以下程序运行后看起来比较有意思,像一个随机选号程序,但不是完全按照问题所说的写的

可供参考,要改很容易

//多线程随机选号程序示例

#include

stdio.h

#include

Windows.h

#include

ctime

#include

cstdlib

#include

process.h

bool

g_run

=

true;

//是否运行

void

userInput(void*)

//监视输入的线程函数

{

while

(true)

{

if

(getchar()=='n')

//是否输入回车

{

g_run

=

!g_run;

//回车运行

回车暂停

}

Sleep(10);

//延迟

}

}

int

main()

{

srand(time(0));

//随机数种子

_beginthread(userInput,0,NULL);

//开线程

while

(true)

{

if

(g_run)

{

system("cls");

//清屏

int

t

=

rand()

%

1000+

1;//1-1000的随机数

printf("n

%d",t);

//输出

}

Sleep(50);

//延迟50毫秒

}

return

0;

}

用C语言写多线程程序

thread_creation.c

gcc thread_creation.c

会在当前目录下,生成可执行的a.out文件

./a.out

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

#includetime.h

DWORD WINAPI ThreadProc1( LPVOID lpParam )

{

int i=0;

time_t timer;

while(1)

{

timer=time(NULL);

printf("The current time is: %sn",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(;;)

{

;

}

}

c语言怎么创建线程和使用?

进程的生命周期:

[1].创建 --- fork 

[2].执行 --- a. exec

b.子进程实现代码逻辑

[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调用一样用来等待指定的线程。下面我们使用一个实例来解释一下使用方法.在实践中,我们经常要备份一些文件。下面这个程序可以实现当前目录下的所有文件备份。

参考资料

CSDN.CSDN [引用时间2018-1-9]

用C语言在windows或者Linux上面,编写一个多线程程序

#includestdio.h

#includestdlib.h

#includewindows.h

DWORD WINAPI ThreadProc(LPVOID lpParam)

{

int *pt=(int*)lpParam;

printf("I am tread %drn",*pt);

}

int main()

{

const int Count=4;

int datas[Count];

DWORD dwThreadId[Count];

HANDLE hThread[Count];

int i;

for(i=0;iCount;i++)

{

datas[i]=i+1;

hThread[i]=CreateThread(NULL,0,ThreadProc,datas[i],0,dwThreadId[i]);

}

WaitForMultipleObjects(Count,hThread,TRUE,INFINITE);

for(i=0;iCount;i++)

{

CloseHandle(hThread[i]);

}

system("PAUSE");

return EXIT_SUCCESS;

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。