LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Is it safe to call a function from two different thread at the same time?

Solved!
Go to solution

For example,

 

thread1()

{

     status = my_function();

 

}

thread2()

{

 

    status = my_function();

}

 

int my_function()

{

    

    do_something;

 

    return result;

}

I want to call my_function from thread1 and thread2 at the same time.

Thanks.
CVI 2010
LabVIEW 2011 SP1
Vision Builder AI 2011 SP1
0 Kudos
Message 1 of 3
(3,957 Views)
Solution
Accepted by test_man

Calling the functions simultaneously, in and of itself, is no problem at all. Each thread has its own call stack.

 

However, you do have examine carefully the do_something part. If you access any global resources (such as hardware devices, global variables or static local variables), or if you make calls to non-thread-safe libraries, then you might need to lock the sensitive bits of code with thread locks, thus ensuring that only one thread can access that code at any one time.

 

Luis

Message 2 of 3
(3,949 Views)

That all depends on what the function does (what's in your "do something").

 

If the function only manipulates or uses resources unique to the thread, then yes, you can have multiple threads active within a function at the same time.  This works because each thread gets its own program counter, registers, and stack.  This is the main idea behind multi-threading, which provides concurrency (true concurrency if mullti-core or multi-processor system) or pseudo-concurrency (single core or hyperthreaded system).

 

When a function uses a resource that can't be shared between threads (e.g. writing to a file, accessing the same global variables, accessing an instrument) then the function must be modified to ensure that only one thread at a time accesses the resource.  You can use mutual exclusion to do this, the Win32 API (assuming you're running on a PC) provides many mechanisms to do this - mutexes, semaphores, critical sections, etc.   NI also provides mechanisms for multi-threading in CVI in a library.  Personally I prefer to use the Win32 API features, but you may find it easier to use the CVI library.

 

Threads have access to all of the process's resources, so you have to watch your step.

 

You may want to refer to a good text such as Beveridge and Wiener ISBN 9780201442342, 1996 and aging but still very good.  You need to understand concurrency fundamentals if you're going to do multi-threading in any significant way.  

 

You can also read up on processes and threads in Win32 in the Win32 SDK provided with the full development CVI system.  If you're running some other OS then the fundamental concepts of concurrency are the same, but the way that you implement of course would be somewhat different.  Unless you're using POSIX threads in which case it might be the same 😉

 

Multi-threading is a powerful tool, and if done well, can greatly improve the effectiveness of your application.  Almost all professionally developed Windows applications are multi-threaded.  But, there is a learning curve, you can create a mess if you don't understand the fundamentals and take precautions to prevent your threads from interfering with each other or worse yet deadlocking one another.

 

There's movement in the micro and compiler business to make multi-threading easier to do - the multi-core micros are only more powerful nowadays for a given application if the app is desgined to use the multiple cores efficiently (i.e. is properly multi-threaded).   Intel's C++ Compiler for Windows will even try to automatically split loops into multiple threads, each working on a piece of the loop, automagically.

 

So I think learning multi-threading fundamentals is essential for any serious developer, especially given the fact that multi-core micros of all types are becoming the norm.

 

Good luck.

 

Menchar

 

 

 

Message Edited by menchar on 07-16-2009 10:27 AM
Message 3 of 3
(3,947 Views)