int a=0

int *p =0

 

p=&a;

 

void * if(void * arg)

{

     int*p = malloc()

 

return p;

}

 

pthread_join( t_id, &p);

 

남의 void 가서 *변수 하면 값을 가져옴

 

void 를 넘길 때 는 주소를 넘겨 (&변수) 받을 그릇을 넘겨야 값을 받아올수있다

 

 

gcc thread.c -o thread -lpthread

이거 엘피 스레드 꼭 써줘야함 주위!!!!!

 

 

 

tread1.c

 

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

void* thread_main(void *arg);

int main(int argc, char *argv[])
{
	pthread_t t_id;
	int thread_param=5;

	if(pthread_create(&t_id, NULL, thread_main, (void*)&thread_param)!=0)
	{
		puts("pthread_create() error");
		return -1;
	};

	sleep(10); puts("end of main");
	return 0;
}

void* thread_main(void *arg)
{
	int i;
	int cnt=*((int*)arg);
	for(i=0;i<cnt;i++)
	{
		sleep(1); puts("running thread");
	}
	return NULL;
}

 

 

 

예는 걍 주고 받는 애

부모쓰레드가 10초 기다려주고

5초 기다리는 자식폼

 

thread2.c

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

void* thread_main(void *arg);

int main(int argc, char *argv[])
{
	pthread_t t_id;
	int thread_param=5;
	void * thr_ret;

	if(pthread_create(&t_id, NULL, thread_main, (void*)&thread_param)!=0)
	{
		puts("pthread_create() error");
		return -1;
	};

	if(pthread_join(t_id, thr_ret)!=0)
	{
		puts("pthread_join() error");
		return -1;
	};

	printf("Thread return message: %s \n", (char*)thr_ret);
	free(thr_ret);
	return 0;
}

void* thread_main(void *arg)
{
	int i;
	int cnt=*((int*)arg);
	char * msg=(char *)malloc(sizeof(char)*50);
	strcpy(msg, "Hello, I'am thread~ \n");
	
	for(i=0; i<cnt; i++)
	{
		sleep(1); puts("running thread");
	}
	return (void*)msg;
}

애는 닫아 주기도 하고

 

 

 

thread3.c

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

void * thread_summation(void * arg);
int sum=0;

int main(int argc, char *argv[])
{
	pthread_t id_t1, id_t2;
	int range1[]={1, 5};
	int range2[]={6, 10};

	pthread_create(&id_t1, NULL, thread_summation, (void *) range1);
	pthread_create(&id_t2, NULL, thread_summation, (void *) range2);
	
	pthread_join(id_t1, NULL);
	pthread_join(id_t2, NULL);
	printf("result: %d \n", sum);
	return 0;
}

void * thread_summation(void * arg)
{
	int start=((int*)arg)[0];
	int end=((int*)arg)[1];
	
	while(start<=end)
	{
		sum+=start;
		start++;
	}
	return NULL;
}

위에 썸이 충돌위험이 있음 왜냐면

전역변수에서 같이 써서 이거 sum 에 같이 드리부움

 

 

thread4.c

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREAD 100

void * thread_inc(void * arg);
void * thread_des(void * arg);
long long num=0;   // long long형은 64 비트 정수 자료형

int main(int argc, char *argv[])
{
	pthread_t thread_id[NUM_THREAD];
	int i;

	printf("sizeof long long: %ld \n", sizeof(long long));
	for(i=0; i<NUM_THREAD; i++)
	{
		if(i%2)
			pthread_create(&(thread_id[i]), NULL, thread_inc, NULL);
		else 
			pthread_create(&(thread_id[i]), NULL, thread_des, NULL);
	}

	for(i=0; i<NUM_THREAD; i++)
		pthread_join(thread_id[i], NULL);

	printf("result: %lld \n", num);
	return 0;
}

void * thread_inc(void * arg)
{
	int i;
	for(i=0; i<50000000; i++)
		num+=1;
	return NULL;
}
void * thread_des(void * arg)
{
	int i;
	for(i=0; i<50000000; i++)
		num-=1;
	return NULL;
}

와 드뎌 최종임 음 왜 갈색이지

 

 

 

스레드를 이용해서 넘겨주고 받고 음 

잘 할당해야지 싸우지않음

 

 

 

애는 오류 날꺼임 이거 해결하는방법임

 

 

'[ 충남인력개발원 ] (2019) > ┗TCP&IP' 카테고리의 다른 글

쓰레드 키주고 받으면서 하기  (0) 2019.11.27
쓰레드 주고 받기 덮어쓰기 없에기  (0) 2019.11.27
프로세스 쓰레드  (0) 2019.11.27
git hub 추가 하기  (0) 2019.11.27
브로드 캐스트  (0) 2019.11.26
블로그 이미지

Or71nH

,