多线程 demo

1.  编译命令  gcc sem.c -lpthread -o sem.out

2.  执行命令 ./sem.out

3.  代码:

/* File sem.c */
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#define MAXSTACK 100
int stack[MAXSTACK][2];
int stack2[MAXSTACK][2];
int size=0;
int size2=0;
sem_t sem;
sem_t sem2;
pthread_mutex_t mut;
pthread_mutex_t mut2;

void ReadData1(void)
{
    printf("ReadData1\n");
    int i=0;
   for (i=0; i<10; i=i+2) {
       pthread_mutex_lock(&mut);
       stack[size][0]=i;
       stack[size][1]=i+1;
       sem_post(&sem);
       
       ++size;
       pthread_mutex_unlock(&mut);
 }
 
}

void ReadData2(void)
{
    printf("ReadData2\n");
    int i=0;
    for (i=10; i<20; i=i+2) {
        pthread_mutex_lock(&mut2);
       stack2[size2][0]=i;
       stack2[size2][1]=i+1;
       sem_post(&sem2);
       
       ++size2;
       pthread_mutex_unlock(&mut2);
    }
    
}

void HandleData1(void)
{
   while(1) {

       sem_wait(&sem);
       pthread_mutex_lock(&mut);
       --size;
       
       printf("Plus:%d+%d=%d\n",stack[size][0],stack[size][1], stack[size][0]+stack[size][1]);
       pthread_mutex_unlock(&mut);
   }
}
void HandleData2(void)
{
   while(1) {
       sem_wait(&sem2);
       
       pthread_mutex_lock(&mut2);
       --size2;
       
       printf("Multiply:%d*%d=%d\n",stack2[size2][0],stack2[size2][1], stack2[size2][0]*stack2[size2][1]);
       pthread_mutex_unlock(&mut2);
   }
}
int main(void)
{
    pthread_t t1,t2,t3,t4;
    sem_init(&sem,0,0);
    sem_init(&sem2,0,0);
    pthread_mutex_init(&mut,NULL);
    pthread_mutex_init(&mut2,NULL);
    pthread_create(&t4,NULL,(void *)ReadData2,NULL);
    pthread_create(&t3,NULL,(void *)ReadData1,NULL);
    
    pthread_create(&t1,NULL,(void *)HandleData1,NULL);
    pthread_create(&t2,NULL,(void *)HandleData2,NULL);

    //pthread_join(t1,NULL);
    pthread_exit(NULL);
}


### 回答1: 好的。 这是一个简单的 Python 多线程示例代码: ``` import threading def worker(): print('I am a worker') threads = [] for i in range(5): t = threading.Thread(target=worker) threads.append(t) t.start() ``` 这段代码会创建 5 个线程,每个线程执行函数 `worker`,该函数会输出 "I am a worker"。 ### 回答2: Python多线程demo是用于展示Python并发编程中多线程的示例代码。 以下是一个简单的Python多线程demo示例: ```python import threading def print_hello(): for _ in range(5): print("Hello from thread {}".format(threading.current_thread().name)) def main(): # 创建两个线程 t1 = threading.Thread(target=print_hello) t2 = threading.Thread(target=print_hello) # 启动线程 t1.start() t2.start() # 等待线程完成 t1.join() t2.join() if __name__ == "__main__": main() ``` 这个示例程序创建了两个线程,并使用`threading.Thread()`方法将`print_hello()`函数分别绑定到每个线程。然后,通过调用`start()`方法启动线程,并使用`join()`方法等待线程完成。 `print_hello()`函数被定义为在每个线程中循环打印一条消息。每个线程会打印出5条消息,通过`threading.current_thread().name`获取当前线程的名称。 通过运行这个示例程序,你可以看到两个线程交替地打印出相同的消息,展示了Python多线程的并发执行的效果。 注意:由于全局解释器锁(GIL)的存在,Python的多线程并不能实现真正的多核并行执行,只能在多核系统中实现任务之间的并发执行。如果要实现真正的并行计算,可以考虑使用多进程。 ### 回答3: Python中的多线程是一种通过同时运行多个线程来执行多个任务的方式。通过使用多线程,可以提高程序的运行效率和响应速度。 下面是一个简单的Python多线程示例,其中创建了两个线程,并且每个线程执行一个不同的任务: ```python import threading # 定义一个线程执行的任务 def thread_task1(): for i in range(5): print("Thread 1 executing") # 定义另一个线程执行的任务 def thread_task2(): for i in range(5): print("Thread 2 executing") # 创建两个线程 thread1 = threading.Thread(target=thread_task1) thread2 = threading.Thread(target=thread_task2) # 启动线程 thread1.start() thread2.start() # 等待线程执行完毕 thread1.join() thread2.join() print("Threads executed completely") ``` 在上面的示例中,我们首先导入了`threading`模块,然后定义了两个函数`thread_task1`和`thread_task2`,分别代表两个线程执行的任务。 接下来,我们使用`threading.Thread`类创建了两个线程对象`thread1`和`thread2`,并指定它们的目标函数为`thread_task1`和`thread_task2`。 然后,我们通过调用`start`方法启动线程,线程会开始执行指定的任务。最后,通过调用`join`方法,主线程会等待所有的子线程执行完毕。 以上就是一个简单的Python多线程示例。通过使用多线程,可以实现多个任务的并发执行,提高程序的运行效率。需要注意的是,在使用多线程时要注意线程之间的同步和资源的共享,以免出现竞争条件和数据不一致的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值