Hi there))
I know that one of the ways to pass variables values into thread -is to create a special structure which includes all needed parameters/

But what about getting values from thread? Is it any way to get value after modification from the thread ?
I know only one - use global variables , but may be there's another one or more that one?

Guys, please tell me about this - if there's something more)

big thanks in advance))

You could use message queues, or shared memory. A class static variable or translation unit static variable could also be used. In any case, there are a lot of ways around this barn, and which may be best for you depends upon a lot of factors, including concurrency, efficiency, and latency.

commented: +++++++++++ +3

ok)) thank you , rubberman))

Threads can return values.

Which API are you using?
Here's an example for PThreads:
https://computing.llnl.gov/tutorials/pthreads/#Joining
Since the thread function is a void pointer, it can point to any piece of data you like, and is retrievable with the pthread_join function.

For Windows, you can use the GetExitCodeThread function.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms683190(v=vs.85).aspx

commented: +++++++++ +3

DeanMSands3 I use windows API .
thanks for references, but how can I use GetExitCodeThread function for example - to

return int i =2

?

As promised:

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

struct sThread{
	LPSECURITY_ATTRIBUTES attr;
	SIZE_T stack;
	LPTHREAD_START_ROUTINE function;
	LPVOID params;
	DWORD flags;
	DWORD id;
	HANDLE handle;
};
typedef struct sThread thread;
struct sAnswer{
	long Number;
};
typedef struct sAnswer answer;

void fromMagratheaWithLove(void *ptr2Answer)
{
	DWORD ptrAsDWORD;
	answer *earth=((answer*)ptr2Answer);
	earth->Number=42;
	ptrAsDWORD=(DWORD)earth;	//Oh yes, I did.
	ExitThread(ptrAsDWORD);
}

thread *lazyThreadConstructor(void (*function)(void*), void *params){
	thread *t=(thread*)malloc(sizeof(thread));
	t->attr=NULL;
	t->stack=0;
	t->function=(LPTHREAD_START_ROUTINE)function;
	t->params=(LPVOID)params;
	t->flags=0;
	t->id=0;
	t->handle=0;
	return t;
}
HANDLE lazyThreadStarter(thread *t){
	t->handle=CreateThread(t->attr,t->stack,t->function,t->params,t->flags,&t->id);
	return t->handle;
}
void lazyThreadDestructor(thread *t){
	t->attr=NULL;
	t->stack=0;
	t->function=NULL;
	t->params=NULL;
	t->flags=0;
	t->id=0;
	t->handle=0;
	free(t);
	t=NULL;
}
int main(int argumentCount,char *arguments[]){
	thread *t;
	answer *toLifeTheUniverseAndEverything;
	answer dontTrustTheMice;
	DWORD slartibartfast;
	t=lazyThreadConstructor(fromMagratheaWithLove, &dontTrustTheMice);
	t->handle=lazyThreadStarter(t);

	WaitForSingleObject( t->handle, INFINITE );
	GetExitCodeThread(t->handle,&slartibartfast);
	toLifeTheUniverseAndEverything=(answer*)slartibartfast;
	printf("The answer to life, the universe, and everything is %ld\n",toLifeTheUniverseAndEverything->Number);
	printf("This tutorial should not be taken seriously.\n");
	CloseHandle(t->handle);
	lazyThreadDestructor(t);
	return 0;
}
commented: ++++++ +3

thank you , DeanMSands3 )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.