linux创建线程和windows创建线程对比

本文介绍了在Linux和Windows环境下如何使用线程进行编程。在Linux部分,通过一个具体的示例展示了如何创建线程并传递参数给线程函数。而在Windows部分,则详细解释了如何创建多个线程,并为每个线程分配不同的任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

linux:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<pthread.h>
 4 
 5 /* 声明结构体 */
 6 struct member
 7 {
 8     int num;
 9     char *name;
10 };     
11 
12 /* 定义线程pthread */
13 static void * pthread(void *arg)       
14 {
15     struct member *temp;
16     
17     /* 线程pthread开始运行 */
18     printf("pthread start!\n");
19     
20     /* 令主线程继续执行 */
21     sleep(2);
22     
23     /* 打印传入参数 */
24     temp = (struct member *)arg;      
25     printf("member->num:%d\n",temp->num);
26     printf("member->name:%s\n",temp->name);
27     
28     return NULL;
29 }
30 
31 /* main函数 */
32 int main(int agrc,char* argv[])
33 {
34     pthread_t tidp;
35     struct member *b;
36 
37     /* 为结构体变量b赋值 */
38     b = (struct member *)malloc(sizeof(struct member));           
39     b->num=1;
40     b->name="mlq";              
41 
42     /* 创建线程pthread */
43     if ((pthread_create(&tidp, NULL, pthread, (void*)b)) == -1)
44     {
45         printf("create error!\n");
46         return 1;
47     }
48     
49     /* 令线程pthread先运行 */
50     sleep(1);
51     
52     /* 线程pthread睡眠2s,此时main可以先执行 */
53     printf("mian continue!\n");
54     
55     /* 等待线程pthread释放 */
56     if (pthread_join(tidp, NULL))                  
57     {
58         printf("thread is not exit...\n");
59         return -2;
60     }
61     
62     return 0;
63 }


windows:

#include <windows.h>
#include <strsafe.h>//win2003SDK必须安装 要不无此头文件。此文件是为了实现StringCchPrintf,StringCchLength。
#define MAX_THREADS    3
#define BUF_SIZE    255
typedef struct _MyData{
int val1;
int val2;
}MYDATA,*PMYDATA;
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
    HANDLE hStdout;
    PMYDATA pData;
    TCHAR msgBuf[BUF_SIZE];
    size_tcchStringSize;
    DWORD dwChars;
    hStdout=GetStdHandle(STD_OUTPUT_HANDLE);
    if(hStdout==INVALID_HANDLE_VALUE)
    return 1;
    //Cast the parameter to the correct data type.
    pData=(PMYDATA)lpParam;
    //Print the parameter values using thread-safe functions.
    StringCchPrintf(msgBuf,BUF_SIZE,TEXT("Parameters=%d,%d\n"),
    pData->val1,pData->val2);
    StringCchLength(msgBuf,BUF_SIZE,&cchStringSize);
    WriteConsole(hStdout,msgBuf,cchStringSize,&dwChars,NULL);
    //Free the memory allocated by the caller for the thread
    //data structure.
    HeapFree(GetProcessHeap(),0,pData);
    return 0;
}
void main()
{
    PMYDATA pData;
    DWORD dwThreadId[MAX_THREADS];
    HANDLE hThread[MAX_THREADS];
    int i;
    //Create MAX_THREADS worker threads.
    for(i = 0; i < MAX_THREADS; i++)
    {
        //Allocate memory for thread data.
        pData=(PMYDATA)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
        sizeof(MYDATA));
        if(pData==NULL)
        ExitProcess(2);
        //Generate unique data for each thread.
        pData->val1=i;
        pData->val2=i+100;
        hThread[i]=CreateThread(
        NULL,//default security attributes
        0,//use default stack size
        ThreadProc,//thread function
        pData,//argument to thread function
        0,//use default creation flags
        &dwThreadId[i]);//returns the thread identifier
        //Check there turn value for success.
        if(hThread[i]==NULL)
        {
            ExitProcess(i);
        }
    }
    //Wait until all threads have terminated.
    WaitForMultipleObjects(MAX_THREADS,hThread,TRUE,INFINITE);
    //Close all thread handle supon completion.
    for(i=0;i<MAX_THREADS;i++)
    {
        CloseHandle(hThread[i]);
    }    
}

http://blog.youkuaiyun.com/cbnotes/article/details/8277180/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值