C multi thread via pthread include pthread_create,pthread_join methods

#include <stdio.h>
#include <stdlib.h>
#include <uuid/uuid.h>
#include <string.h>
#include <pthread.h>

void *pMsg3(void *ptr);
void pthread8();

int main()
{
    pthread8();
    return 0;
}

void pthread8()
{
    pthread_t t1,t2;
    int ret1,ret2;
    char *msg1="Pthread_create(&t1,NULL,printMsg,(void*)msg1);";
    char *msg2="pthread_join(t1,NULL)";
    
    ret1=pthread_create(&t1,NULL,pMsg3,(void*)msg1);
    ret2=pthread_create(&t2,NULL,pMsg3,(void*)msg2);

    pthread_join(t1,NULL);
    pthread_join(t2,NULL);

    printf("Ret1=%d\n",ret1);
    printf("Ret2=%d\n",ret2);
}

void *pMsg3(void *ptr)
{
    char *mgs=(char*)ptr;
    printf("Str=%s\n",mgs);
}

The 2 critical methods are thread creation and join as below.

pthread_create(&t1,NULL,MethodName,(void*)args);

pthread_join(t1,NULL);

 

Compile the c program via gcc must include -lpthread instructions;

gcc -g h1.c -o h1 -lpthread -luuid

Or

gcc h1.c -o h1 -lpthread -luuid

 

 

posted @ 2021-11-26 09:31  FredGrit  阅读(34)  评论(0编辑  收藏  举报