C语言创建多线程

      线程是计算机中独立运行的最小单位。每个线程占用的CPU时间是由系统分配的,因此可以把线程看成操作系统分配CPU时间的基本单位。每个线程只有在系统分配给它的时间片内才能取得CPU控制权,执行线程中的代码。

      Linux操作系统在一个进程内生成多个线程。多线程和多进程相比,拥有以下优点:


(1)进程都有独立的地址空间,创建新进程要耗费时间为期分配系统资源,而线程共享进程的地址空间,所以创建线程花费的时间要少得多。

(2)系统调度方面,由于进程地址空间独立而线程共享地址空间,所以线程间的切换速度要远远快过进程间的切换速度。

(3)通信机制方面,进程间的数据空间相互独立,彼此通信要以专门的通信方式进行,且必须经过操作系统。而多线程共享共享数据空间,一个线程的数据可以直接提供给其他线程使用,而不必经过操作系统。因此,线程间的通信更加方便省时。


      虽然线程在进程内部共享地址空间、打开的文件描述符等资源。但是线程也有其私有的数据信息,包括:

(1)线程ID:每个线程都由一个唯一的线程号。

(2)寄存器(包括程序计数器和堆栈指针)。

(3)堆栈

(4)信号掩码

(5)优先级

(6)线程私有的存储空间


      Linux下的多线程程序,需要使用头文件pthread.h,链接时需要使用库libpthread.a。

      在主线程里创建线程,程序就会在创建线程的地方产生分支,变成两个程序执行。这似乎和多进程一样,其实不然。子进程时通过拷贝父进程的地址空间来实现的;而线程与进程内的其他线程共享程序代码,一段代码可以同时被多个线程执行。

      线程的创建通过函数pthread_create来完成,声明如下:

#include <pthread.h>

int pthread_create(pthread_t *thread, pthread_attr_t * attr, void* (*start_routine)(void *),void *arg);

  • thread: 是一个指针,线程创建成功时,用以返回创建的线程ID
  • attr:指定线程属性,NULL表示使用默认属性
  • start_routine:函数指针,指向线程创建后要调用的函数。这个被线程调用的函数也称为线程函数。
  • arg:该参数指向传递给线程函数的参数。
      线程创建成功时,pthread_create函数返回0,新创建的线程开始运行第3个参数所指向的函数,原来的线程继续运行。


线程的创建过程:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

int * thread(void * arg)
{
	pthread_t newthid;

	newthid = pthread_self();
	printf("this is a new thread, thread ID = %d\n", newthid);
	return NULL;
}

int main(void)
{
	pthread_t thid;

	printf("main thread ,ID is %d\n",pthread_self());
	if(pthread_create(&thid, NULL, (void *)thread, NULL) != 0) {
		printf("thread creation failed\n");
		exit(1);
	}
	exit(0);
}

运行结果:


  • 17
    点赞
  • 90
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
### 回答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语言如何创建和管理多个线程的基本操作。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值