Click here to Skip to main content
15,066,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
i am new in multithreading dont get this point how to do .
i need help how inside a thread a function which is used for threading is called plz help i explain in comments in sample code where i need help




C#
file *sync;
char temp[10];// filled by some data later and passed as argument inside the func

criticalSection cs; 

void TH1(void *param)// how this thread recive dataOfFunc() with arguments 
{

enterCriticalSection(&cs);
dataOfFunc(sync,tempArray);
Leave Critical Section(&cs); 

}

void main
{
initializeCriticalSection(&cs);
beginThread(TH1,0,---------);  // here i want to know how to pass function nameand its parameterinside beginthread third variable

enterCriticalSection
dataOfFunc(sync,tempArray);
Leave Critical Section (&cs)
} 

void dataOfFunc(File *sync ,char [])// function which is to be called in threads
{
// all code to perform operation
}
Posted
Updated 26-Jan-16 18:03pm
v2

A function is not something which needs to be "passed". Perhaps you don't quite understand what a thread is. A function, generally, can be called in any thread, and not in only one thread, but its code should be thread-safe. One of the best ways to do it is doing all computation of the stack. "No synchronization is the best synchronization".

However, thread safety, possible use of thread synchronization primitives and resources shared between thread is hardly a good topic for a single forum question. Rather, you should systematically learn all these aspects. You could understand big part of it even without reading on specific parallelism theory, but just by learning how a computer and software work on a low level, first of all, CPU and memory operation, CPU task context and, most importantly, call stack.

If you, for example, could provide some code sample of a function which is already written and proven to work in just one certain thread but you had some concerns related to its operation in multithreading situation, it would be a different story. We could discuss the particular detail related to this function. Covering the matter on so abstract level applicable to all possible functions would be a topic which is too broad for a forum; which could not replace a systematic study.

—SA
   
The _beginthread, _beginthreadex[^] MSDN page contains example code that covers also passing arguments to the thread.
   
I'm going to give you a brief answer since I only have a minute...

In functions where you only have one parameter available to pass information through, you typically use structures. Define a structure that includes everything you want to the function and pass a pointer to the structure, within the function of interest, cast your structure to the expected structure type, and there you have it... multiple arguments being passed within a single parameter.

There are other ways to do this... notably, using something like boost::bind (or std::bind in C++11) to match the arguments to variables so your prototype still has as many arguments as you'd expect.

Happy coding!
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900