小白初识linux下的C语言多线程

简介: 小白初识linux下的C语言多线程

欢迎

下面举例 :创建52个线程

一个教授,一个助教,50个学生

#include <unistd.h>     /* Symbolic Constants */
#include <sys/types.h>  /* Primitive System Data Types */ 
#include <errno.h>      /* Errors */
#include <stdio.h>      /* Input/Output */
#include <stdlib.h>     /* General Utilities */
#include <pthread.h>    /* POSIX Threads */
#include <string.h>     /* String handling */
#include <time.h>
/* prototype for thread routine */
void print_message_function ( void *ptr );
/* struct to hold data to be passed to a thread
   this shows how multiple data items can be passed to a thread */
typedef struct str_thdata
{
    int  thread_no;
    char message[100];
}thdata;
int main()
{
    pthread_t thread[52];  /* thread variables */
    thdata data[52];         /* structs to be passed to threads */
    /* initialize data to pass to thread 1 */
    data[0].thread_no = 1;
    strcpy(data[0].message, "TY doctor!");
    /* initialize data to pass to thread 2 */
    data[1].thread_no = 2;
    strcpy(data[1].message, "yang teaching assistant!");
    /* initialize data to pass to thread 3-52 */
    for(int i=2;i<52;i++){
    data[i].thread_no = i+1;
    strcpy(data[i].message,"student");
    }
    srand((int)time(NULL));
    /* create threads 1 and 2 */    
    pthread_create (&thread[0], NULL, (void *) &print_message_function, (void *) &data[0]);
    usleep(1);
    pthread_create (&thread[1], NULL, (void *) &print_message_function, (void *) &data[1]);
  usleep(1);
  for(int i=2;i<52;i++){
    pthread_create (&thread[i], NULL, (void *) &print_message_function, (void *) &data[i]);
    int m=rand()%200;
    usleep(m);
  }
    /* Main block now waits for all threads to terminate, before it exits
       If main block exits, both threads exit, even if the threads have not
       finished their work */ 
    for(int j=0;j<52;j++)
      pthread_join(thread[j], NULL);
    /* exit */  
    exit(0);
} /* main() */
/**
 * print_message_function is used as the start routine for the threads used
 * it accepts a void pointer 
**/
void print_message_function ( void *ptr )
{
    thdata *data;            
    data = (thdata *) ptr;  /* type cast to a pointer to thdata */
    /* do the work */
    if(data->thread_no>2){
      printf("Thread %2d says %s %2d\n", data->thread_no, data->message,(data->thread_no-2));
    }
    else{
        printf("Thread %2d says %s \n", data->thread_no, data->message);
    }
    pthread_exit(0); /* exit */
} /* print_message_function ( void *ptr ) */


输出样例

$ gcc -o main main.c -lpthread
$ ./main
Thread  1 says doctor! 
Thread  2 says teaching assistant! 
Thread  3 says student  1
Thread  4 says student  2
Thread  5 says student  3
......
Thread 51 says student 49
Thread 52 says student 50


相关文章
|
10月前
|
存储 Linux API
【Linux进程概念】—— 操作系统中的“生命体”,计算机里的“多线程”
在计算机系统的底层架构中,操作系统肩负着资源管理与任务调度的重任。当我们启动各类应用程序时,其背后复杂的运作机制便悄然展开。程序,作为静态的指令集合,如何在系统中实现动态执行?本文带你一探究竟!
【Linux进程概念】—— 操作系统中的“生命体”,计算机里的“多线程”
|
8月前
|
并行计算 Linux
Linux内核中的线程和进程实现详解
了解进程和线程如何工作,可以帮助我们更好地编写程序,充分利用多核CPU,实现并行计算,提高系统的响应速度和计算效能。记住,适当平衡进程和线程的使用,既要拥有独立空间的'兄弟',也需要在'家庭'中分享和并行的成员。对于这个世界,现在,你应该有一个全新的认识。
288 67
|
10月前
|
Linux
Linux编程: 在业务线程中注册和处理Linux信号
通过本文,您可以了解如何在业务线程中注册和处理Linux信号。正确处理信号可以提高程序的健壮性和稳定性。希望这些内容能帮助您更好地理解和应用Linux信号处理机制。
189 26
|
10月前
|
Linux
Linux编程: 在业务线程中注册和处理Linux信号
本文详细介绍了如何在Linux中通过在业务线程中注册和处理信号。我们讨论了信号的基本概念,并通过完整的代码示例展示了在业务线程中注册和处理信号的方法。通过正确地使用信号处理机制,可以提高程序的健壮性和响应能力。希望本文能帮助您更好地理解和应用Linux信号处理,提高开发效率和代码质量。
191 17
|
安全 Java C语言
C语言线程解池解读和实现01
C语言线程解池解读和实现01
|
消息中间件 Unix Linux
【C语言】进程和线程详解
在现代操作系统中,进程和线程是实现并发执行的两种主要方式。理解它们的区别和各自的应用场景对于编写高效的并发程序至关重要。
407 6
|
消息中间件 存储 负载均衡
C 语言多线程编程:并行处理的利剑
C语言多线程编程是实现并行处理的强大工具,通过创建和管理多个线程,可以显著提升程序执行效率,尤其在处理大量数据或复杂计算时效果显著。
|
资源调度 Linux 调度
Linux C/C++之线程基础
这篇文章详细介绍了Linux下C/C++线程的基本概念、创建和管理线程的方法,以及线程同步的各种机制,并通过实例代码展示了线程同步技术的应用。
227 0
Linux C/C++之线程基础
|
网络协议 C语言
C语言 网络编程(十四)并发的TCP服务端-以线程完成功能
这段代码实现了一个基于TCP协议的多线程服务器和客户端程序,服务器端通过为每个客户端创建独立的线程来处理并发请求,解决了粘包问题并支持不定长数据传输。服务器监听在IP地址`172.17.140.183`的`8080`端口上,接收客户端发来的数据,并将接收到的消息添加“-回传”后返回给客户端。客户端则可以循环输入并发送数据,同时接收服务器回传的信息。当输入“exit”时,客户端会结束与服务器的通信并关闭连接。
|
存储 Ubuntu Linux
C语言 多线程编程(1) 初识线程和条件变量
本文档详细介绍了多线程的概念、相关命令及线程的操作方法。首先解释了线程的定义及其与进程的关系,接着对比了线程与进程的区别。随后介绍了如何在 Linux 系统中使用 `pidstat`、`top` 和 `ps` 命令查看线程信息。文档还探讨了多进程和多线程模式各自的优缺点及适用场景,并详细讲解了如何使用 POSIX 线程库创建、退出、等待和取消线程。此外,还介绍了线程分离的概念和方法,并提供了多个示例代码帮助理解。最后,深入探讨了线程间的通讯机制、互斥锁和条件变量的使用,通过具体示例展示了如何实现生产者与消费者的同步模型。