C语言中多线程的简单例子

    2月初的时候接到一个需求,简单点说就是做个动画。

    运用多线程的技术,当动画启动的时候,不影响其他画面的表示和押键响应。

    A 线程启动后,在某个阶段启动了B线程。

  A这里使用main()函数,B这里使用让动画'动'起来的线程animation,那么可以这么做,直接看代码。

   

#include <stdio.h>
#include <windows.h>
DWORD WINAPI  animation();
int main()
{
	char endFlag = 0;
	CreateThread( 
		NULL              // default security attributes
		,0                 // use default stack size  
		,animation       // thread function 
		,NULL             // argument to thread function 
		,0                 // use default creation flags 
		,NULL);           // returns the thread identifier 

	while (1){
		int i;
		printf ("A还在运行中!!!\n\n\n");
		for(i = 0; i < 200000000; i++){
			;
		}
	}
	return 0;
}


DWORD WINAPI  animation()
{
	int pic[4] = {1,2,3,4};
	int i = 0;
	printf ("在这里进行图片切换!\n");
	while (1){
	int j = 0;
		if (i < 4){
			int currentPic = pic[i];
			printf ("当前是第%d帧\n",currentPic);
			i++;
			for(j = 0; j < 200000000; j++){
				;
			}

		} else if (i >= 4) {
			i = 0;
		}
		
	}
}

 

     在main()里面有一个for(i = 0; i < 200000000; i++){;},在真实的项目中,这里的大括号中多用来进行押键的回调函数,这样主线程永远不会退出。在animation函数中,有一个for(j = 0; j < 200000000;j++){;},真实项目中,这里一般是计算你动画要动多快的时间,使用sleep(时间间隔);来确定。

      新创建的线程什么时候结束?线程间怎么进行通信?线程内部是否能结束自己,外部呢?这些问题接下来研究。

转载于:https://www.cnblogs.com/arrow12/archive/2012/02/16/2355000.html

### 回答1: 下面是一个简单的使用 C 语言创建多线程例子: ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> void *thread_func(void *arg) { int i; for (i = 0; i < 10; i++) { printf("Thread says hi!\n"); sleep(1); } pthread_exit(NULL); } int main() { pthread_t my_thread; int ret; ret = pthread_create(&my_thread, NULL, thread_func, NULL); if (ret != 0) { printf("Error creating thread!\n"); exit(EXIT_FAILURE); } printf("Main function says hi!\n"); pthread_join(my_thread, NULL); return 0; } ``` 在这个例子,我们使用了 `pthread_create()` 函数来创建一个新的线程,并传递给它一个指向 `thread_func()` 函数的指针作为参数。在 `thread_func()` 函数,我们使用 `printf()` 函数输出一条消息,并使用 `sleep()` 函数暂停一秒钟,以便我们可以看到线程运行的效果。在主函数,我们使用 `pthread_join()` 函数等待线程完成,并在输出一条消息后退出程序。 ### 回答2: 以下是一个用C语言创建多线程例子: ```c #include <stdio.h> #include <pthread.h> #define NUM_THREADS 5 // 线程函数 void *printHello(void *threadID) { long tid = (long)threadID; printf("Hello from thread %ld\n", tid); pthread_exit(NULL); } int main() { pthread_t threads[NUM_THREADS]; int rc; long t; // 创建多个线程 for (t = 0; t < NUM_THREADS; t++) { printf("Creating thread %ld\n", t); rc = pthread_create(&threads[t], NULL, printHello, (void *)t); if (rc != 0) { printf("Error creating thread. Return code: %d\n", rc); return -1; } } // 等待所有线程结束 for (t = 0; t < NUM_THREADS; t++) { rc = pthread_join(threads[t], NULL); if (rc != 0) { printf("Error joining thread. Return code: %d\n", rc); return -1; } } printf("All threads completed.\n"); return 0; } ``` 以上代码创建了5个线程,每个线程打印出自己的线程ID。在主函数,首先创建了5个线程,并且通过`pthread_create`函数来指定线程执行的函数为`printHello`,并传递每个线程对应的线程ID作为参数。然后通过`pthread_join`函数等待每个线程结束。最后输出"All threads completed."表示所有线程执行完毕。 ### 回答3: C语言创建多线程的一个简单例子如下所示: #include <stdio.h> #include <stdlib.h> #include <pthread.h> // 回调函数,用于线程执行 void *thread_function(void *arg) { int *thread_id = (int *)arg; printf("线程 %d 正在执行\n", *thread_id); // 执行其他操作... printf("线程 %d 执行完毕\n", *thread_id); pthread_exit(NULL); } int main() { pthread_t thread1, thread2; int id1 = 1, id2 = 2; // 创建线程1 if (pthread_create(&thread1, NULL, thread_function, (void *)&id1) != 0) { fprintf(stderr, "无法创建线程1\n"); return 1; } // 创建线程2 if (pthread_create(&thread2, NULL, thread_function, (void *)&id2) != 0) { fprintf(stderr, "无法创建线程2\n"); return 1; } // 等待线程1执行完毕 if (pthread_join(thread1, NULL) != 0) { fprintf(stderr, "无法等待线程1执行完毕\n"); return 1; } // 等待线程2执行完毕 if (pthread_join(thread2, NULL) != 0) { fprintf(stderr, "无法等待线程2执行完毕\n"); return 1; } printf("所有线程执行完毕\n"); return 0; } 这个例子创建了两个线程,每个线程都执行了相同的回调函数`thread_function`。在主函数,我们利用`pthread_create`函数创建了线程,并使用`pthread_join`函数等待线程执行完毕。每个线程都传递了一个整数作为参数,用于标识不同的线程。在回调函数,我们通过强制转换将参数指针转换为整数,并在控制台打印出相应的线程标识。最后,主线程等待两个子线程执行完毕后,打印了"所有线程执行完毕"的消息。这个例子展示了C语言如何创建和管理多个线程的基本操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值